Next.js Discord

Discord Forum

Cache between authenticated users

Unanswered
Curly-Coated Retriever posted this in #help-forum
Open in Discord
Curly-Coated RetrieverOP
I've a doubt about cache. How can I share it between authenticated users? I've a request with same response for authenticated users and I want to cache it. How can I do it? Because the Authorization on headers is used for cache key. I've searched a lot and I haven't found an answer about it.

I've found some github issues without final answer from vercel team.

https://github.com/vercel/next.js/discussions/51279
https://github.com/vercel/next.js/discussions/76875

A similar scenario An authenticated store, where the product data is the same for all users but confidential. Users must be authenticated to view it, but I’d like to cache the response to avoid hitting the backend on every request.

How to handle it? It's a common case for me and seems anyone cared about it.

7 Replies

@Curly-Coated Retrieversolved?
@B33fb0n3 > Though whether or not using auth header as cacheKey is a good idea or not is a separate concern if I would be you, I wouldn't do that. The fear or leaking personal and secret data is bigger than the advantage this brings. Instead I would use `'use cache: private'` to do stuff like that
I really like to learn about stuff like this so I dont want to sounds like jerk,so plz correct me if im wrong, but from what I read 'use cache: private' is Per-client (browser) caching. I would really like to know more about caching after auth between users, so the page can be pre rendered like when using 'use cache' across deployments
@B33fb0n3 I was thinking use cache: remote might work but it says it has to be passed the cookie or headers as arguments, so I was wondering if you could use cache remote if the parent function still checks auth
I just tested it on a simple project and using 'use cache: remote' I was able to fetch data with a fall back then log in with a different user and there was no fall back shown, I found this page that really helped: https://nextjs.org/docs/app/api-reference/directives/use-cache-remote#how-use-cache-remote-differs-from-use-cache-and-use-cache-private