Next.js Discord

Discord Forum

NextJS 13/14 caching...

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
Hi All,

Trying to understand how caching works...

Imagine a notes app where each authenticated user can login and view at a /notes route in the /app directory containing their private notes. That route has a /app/notes/page.tsx that is a Server component which queries the user's notes from a database and renders them in a list. Of course, caching is enabled by default. The user can add a new note and do a revalidatePath('/notes') to invalidate the cache. If we wanted to cache the fetches from the database, we can of course wrap the fetching function in unstable_cache(async () => ...).

Now, when another authenticated user (User B) is logged in, what stops User B from seeing the cached page containing User A's notes ?

I understand for a use-case where authorization is not an issue (e.g., a public blog), it's helpful to cache across user sessions.

But if the server component is rendering private information specific to each user, as in the case above, caching would have to be done on a per-user / per-session basis. If not, then it would mean we have to invalidate the cache at each request (
export const revalidate=0
) which limits the performance when a user is accessing their own /notes page multiple times.

0 Replies