07887 423199me@richieaubin.com

Set cache headers without mixing page variants

Cache the responses you are happy to reuse. Before extending a cache lifetime, check whether the same URL serves different output to different visitors.

HTTP cache policy example
Example image · CC0 1.0 licence

Choose a way to do this check

Browser Network

  1. Load a public URL twice and note the response and cache headers. Repeat with the intended language setting.
  2. Check the actual language and content, not only whether the cache records a hit.

PageSpeed Insights and Lighthouse

  1. Use lab diagnostics to identify static resources with short cache lifetimes.
  2. Confirm the headers directly before changing rules. Review field metrics separately from the lab score.
  3. For repeatable lab results, use Chrome DevTools Lighthouse. Repeat the audit with the same device and settings and compare the middle result.
  4. If the URL lacks real-user data, record the origin scope separately. Use the lab reports to see whether the delivery change plausibly improved loading.

Read cache headers

  1. Paste a response from each approved language or session fixture.
  2. Compare Vary, Cache-Control and Age, and test that personalised content is not reused.

Follow the illustrated walkthroughs for Chrome Lighthouse.

1. Separate public and personalised responses

A versioned image or CSS file can often have a long public lifetime when changing its contents also changes its URL. Public HTML may need a shorter policy if availability, prices or editorial content change regularly. A personalised account page should not be reused by a shared cache. Start with the most restrictive policy needed for each class of response, then confirm how your CDN interprets it.

A private response can still be stored by a private cache unless other directives prevent it. Use no-store for a response you want caches to avoid storing. Do not rely on a cookie-based cache key to protect personal data. Avoid broad public rules for HTML where the application can serve authenticated output.

2. Send the right Cache-Control header at the right layer

The illustrative PHP fragments belong in different handlers. Use the first for a public asset only if the filename changes when the bytes change. Use the second for a personalised route. Send headers before any body output and check for rules in front of the application that might replace them.

PHP · versioned public asset handler
<?php
header('Cache-Control: public, max-age=31536000, immutable');
PHP · personalised response handler
<?php
header('Cache-Control: private, no-store');

3. Use Vary for a real request-header variation

When the server deliberately uses Accept-Language to select a public response for one URL, Vary should name that header so caches can distinguish those requests. Prefer distinct locale URLs where practical. Preserve other applicable fields such as Accept-Encoding if content coding depends on it. A Vary header is not a substitute for a trustworthy CDN cache configuration or explicit URL routing.

HTTP response · when content varies by Accept-Language
Cache-Control: public, max-age=300
Vary: Accept-Language

4. Verify matching and non-matching cases

Fetch two variants in both orders, and compare the visible output, canonical and indexing instructions. Check the first and repeat request for each. The Age or CDN-specific cache headers help explain what happened. Serving the same English template to two languages could be expected if the URL has fixed content. Use the policy you intended as the comparison.

Terminal · save two public response variants
url='https://www.example.com/guides/'
curl -sS -H 'Accept-Language: en-GB' -D en.txt -o en.html "$url"
curl -sS -H 'Accept-Language: fr-FR' -D fr.txt -o fr.html "$url"
grep -Ei '^(HTTP/|age:|vary:|cache-control:|content-language:)' en.txt fr.txt

Use approved test accounts when verifying personal pages. Run the same journey logged out and confirm no personalised content crosses sessions. If an edge rule serves the wrong version, remove the affected rule or disable caching for the route and purge its cached responses through the authorised procedure. Confirm correct output before re-enabling caching.

Technical references

Frequently asked questions

Does no-cache mean no storage?

No. It generally requires validation before a stored response is reused. Use no-store where storage should be avoided.

Can Vary replace separate locale URLs?

It can separate cached variants but does not provide distinct URLs for visitors or search engines.

Should I apply immutable to HTML?

Usually reserve it for resources whose URL changes when the content changes.

What does Age show?

It indicates the cache age reported for a response. It does not prove the cached variant is correct.

Written by Richie Aubin

Published