Cache between authenticated users
Unanswered
Curly-Coated Retriever posted this in #help-forum
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.
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.
2 Replies
@Curly-Coated Retriever 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.
You can definetly pass the auth header as the parameter of the cache key. If you make a function that accepts auth header, DataCache will serialize the parameter and uses it as cacheKey.
Though whether or not using auth header as cacheKey is a good idea or not is a separate concern
Though whether or not using auth header as cacheKey is a good idea or not is a separate concern
@Curly-Coated Retriever 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.
Though whether or not using auth header as cacheKey is a good idea or not is a separate concernif 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