Back to results for “Security”
Next.js › Caching

What caching pitfalls should you watch out for when building a Next.js application with dynamic, user specific data?

Advanced

Answer

A common pitfall is accidentally caching data that is different for each user, such as personalized dashboards or shopping carts, which can leak one user's data to another. You avoid this by using cache: no-store or cookies based rendering for anything personalized, while still caching truly shared, public data.
// Personalized data should not be cached
const res = await fetch('https://api.example.com/cart', { cache: 'no-store' });
Real-world example A shopping cart feature accidentally cached one customer's cart contents and briefly showed it to another customer, so the team fixed it by explicitly disabling caching for anything tied to an individual user's session.

Common follow-ups: How do you test that personalized data is not being accidentally cached?;What tools can help detect caching bugs before they reach production?