User Agents

Web browsers and everyday web tech, explained

Caching Customized Pages Without Leaking User Data

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Caching customized pages without leaking user data is achieved by separating static page structures from dynamic, user-specific content. By using a combination of shared caching for the layout and client-side fetching or fragmented caching for personal data, you ensure that one user's private information is never stored in a public cache and served to another visitor. This approach maintains high performance and low server load while upholding strict data privacy standards.

What You'll Need

  • A web server with caching capabilities (e.g., Nginx, Varnish, or a CDN like Cloudflare).
  • A frontend framework or vanilla JavaScript for asynchronous data loading.
  • An API endpoint that serves user-specific data via JSON.
  • Access to HTTP header configurations for your application.

Step-by-Step

  1. Identify Static vs. Dynamic Elements: Audit your customized pages to determine which parts are identical for all users (the shell, CSS, navigation, footer) and which parts are unique (username, account balance, shopping cart, profile settings).
  2. Create a Generic Page Template: Remove all user-specific data from the server-side HTML render. Replace these areas with "placeholder" elements or loading skeletons. This allows the page to be completely generic and safe for public caching.
  3. Configure Cache-Control Headers: Set the Cache-Control header for the generic page to public, max-age=3600 (or your preferred duration). This tells the CDN and browser that this specific version of the page is not private and can be shared across different users.
  4. Implement Client-Side Data Fetching: Write a JavaScript function that executes after the page loads in the browser. This script should make an authenticated request (using a secure cookie or JWT) to your API to fetch the user's specific data.
  5. Inject Data into the DOM: Once the API returns the JSON response, use JavaScript to populate the placeholder elements identified in step 2 with the actual user data. Because this happens in the user's browser, the private data never enters the shared cache.
  6. Secure the API Endpoints: Ensure your data API uses Cache-Control: no-store, no-cache, must-revalidate. This prevents the browser or any intermediate proxy from caching the JSON response containing sensitive information.
  7. Implement Vary Headers: If you must cache different versions of a page based on a user's role (e.g., Admin vs. Member) rather than individual identity, use the Vary: Cookie or Vary: X-User-Role header. This tells the cache to store separate versions of the page based on the value of that specific header.

Common Mistakes

  • Caching the Full Page with Cookies: Setting a page to public
  • Over-reliance on Vary: Cookie: Using Vary: Cookie on a site where every user has a unique session ID effectively disables caching, as the cache will create a new entry for every single visitor, filling up storage without providing performance gains.
  • Ignoring the API Cache: Focusing entirely on the HTML page but forgetting to disable caching on the JSON API endpoints, which can lead to private data being stored in local browser caches or corporate proxies.
  • Client-Side Only Security: Relying on JavaScript to "hide" data that was actually sent in the HTML. If the data is in the source code, it is cached; hiding it with CSS or JS does not prevent the leak.

Tips

  • Use Skeleton Screens: Instead of a blank space while the user data loads, use gray animated blocks (skeletons) to improve the perceived performance and reduce layout shift.
  • Edge Side Includes (ESI): If your CDN supports ESI, you can define small fragments of a page as "private" while the rest remains "public." The CDN assembles the page at the edge, fetching the private fragment from your server.
  • Local Storage for Non-Sensitive Preferences: For non-sensitive customizations (like Dark Mode or language preference), store these in localStorage and apply them via JS to avoid needing a server-side check.
  • Audit with Incognito Mode: Regularly test your cached pages in an incognito window to ensure no remnants of a previous session's data are appearing.

Troubleshooting

  • User data is not appearing: Check the browser console for CORS errors or 401 Unauthorized responses. Ensure the API request is sending the correct authentication tokens.
  • Old data is showing after a profile update: This is likely a client-side caching issue. Ensure your API response includes Cache-Control: no-cache or use a cache-busting query string (e.g., /api/user?t=1234567).
  • The page is not caching at all: Check if your server is sending a Set-Cookie header on the generic page. Many CDNs automatically bypass the cache if they see a Set-Cookie header to avoid accidentally caching session IDs.
  • Layout shifts (CLS) are too high: If the page jumps when user data loads, set fixed heights and widths for your placeholder elements so the content doesn't push other elements down.

Frequently Asked Questions

What is the difference between public and private caching?

Public caching allows any cache (CDN, proxy, browser) to store the response and serve it to any user. Private caching restricts storage to the end-user's browser only, ensuring other users cannot see that specific response.

Can I use cookies to determine what to cache?

Yes, but only for broad categories (like "logged-in" vs "guest"). Using cookies for individual user identification in a shared cache is dangerous and usually results in a cache miss for every request.

Is client-side fetching slower than server-side rendering?

The initial page load is faster because the HTML is served instantly from a cache. However, there is a slight delay before the personalized data appears. This is generally preferred over a slow server-side render.

What happens if the API fails to load the user data?

You should implement a fallback mechanism, such as a "Retry" button or a graceful degradation that shows a generic version of the page without the personalized elements.

Does this method work for SEO?

Yes. Search engine crawlers typically do not have user sessions, so they will see the generic, cached version of the page. Since personalized data is usually not the primary content intended for indexing, this does not negatively impact SEO.