Optimizing Fetch Cache for User-Specific Data in Next.js: Best Practices?
Answered
Marble gall posted this in #help-forum
Marble gallOP
Hello everyone,
I'm working on a Next.js project with a private user area and I'm contemplating the best approach to cache server-side requests for authenticated user data.
Here's a scenario for clarity:
Public Access: Non-authenticated users fetch articles, caching them with an "articles" tag.
Editor Action: When an editor publishes a new article, we invalidate the "articles" tag using revalidateTag, prompting a fresh fetch for non-authenticated users.
Now, consider adding a subscription feature:
Authenticated Access: Users fetch their subscriptions, caching them with a "subscriptions" tag.
Subscription Update: A user modifies their subscription, invalidating the "subscriptions" tag. This action causes the cache to become stale for all users, leading to unnecessary data refetching.
A potential solution is to include the user ID in the cache tag (e.g., "subscriptions/<user_id>"), but this raises concerns about cache management and data clearing on the Next.js server. Specifically:
- How efficiently can Next.js handle numerous user-specific cache tags?
- Is it possible to combine tag-based and time-based revalidation for optimal performance?
I'm eager to hear your thoughts and suggestions on managing cache for user-specific data in Next.js, particularly concerning scalability and efficiency.
Thank you!
I'm working on a Next.js project with a private user area and I'm contemplating the best approach to cache server-side requests for authenticated user data.
Here's a scenario for clarity:
Public Access: Non-authenticated users fetch articles, caching them with an "articles" tag.
Editor Action: When an editor publishes a new article, we invalidate the "articles" tag using revalidateTag, prompting a fresh fetch for non-authenticated users.
Now, consider adding a subscription feature:
Authenticated Access: Users fetch their subscriptions, caching them with a "subscriptions" tag.
Subscription Update: A user modifies their subscription, invalidating the "subscriptions" tag. This action causes the cache to become stale for all users, leading to unnecessary data refetching.
A potential solution is to include the user ID in the cache tag (e.g., "subscriptions/<user_id>"), but this raises concerns about cache management and data clearing on the Next.js server. Specifically:
- How efficiently can Next.js handle numerous user-specific cache tags?
- Is it possible to combine tag-based and time-based revalidation for optimal performance?
I'm eager to hear your thoughts and suggestions on managing cache for user-specific data in Next.js, particularly concerning scalability and efficiency.
Thank you!
Answered by Marble gall
I think I don't, after some research I found that user specific data should not be cached at all on the server (this is the default behavior of Next). To build user dashboards I will use SWR as I always did 🙂
4 Replies
Tomistoma
Did you end up solving this? Because this is the exact issue I'm currently facing.
@Marble gall how many requests are you expecting to be getting daily? Do you need this level of control?
@Tomistoma Did you end up solving this? Because this is the exact issue I'm currently facing.
Marble gallOP
I just answered on Github! https://github.com/vercel/next.js/discussions/59527
@qoobes <@835564062271602710> how many requests are you expecting to be getting daily? Do you need this level of control?
Marble gallOP
I think I don't, after some research I found that user specific data should not be cached at all on the server (this is the default behavior of Next). To build user dashboards I will use SWR as I always did 🙂
Answer