Next.js · Senior · Question 42 of 75
How do you make sure a 'use cache' function doesn't cache different users' data together?
- ✓Correct answer: Pass the user ID as an argument: it becomes part of the key
- 2Call
cookies()inside the function to get the id - 3Use one global key and clear the cache manually
Explanation
The cache key is built from arguments and captured values, so getDashboard(userId) creates separate entries. Reading cookies() inside a cached function is forbidden. Note that personal data in a shared server cache is a reason to consider 'use cache: private'.