Express.js / Intermediate: modules, identity and adapters / Lesson 14
Conditional reads and private caching
Use HTTP validators deliberately while preserving authorization and representation-specific cache boundaries.
On this page
Lesson goals
- Explain a validator and a conditional read.
- Set a private cache policy and inspect freshness.
- Test changed data, user scope and proxy behavior.
Define the cached representation
A cache validator identifies a particular representation so a client can ask whether its stored copy is still usable. The response also needs a cache policy that matches its sensitivity and intended lifetime. Authorize access before evaluating freshness: a matching validator does not grant permission to read a protected record. A shared cache must not serve one userโs private representation to another.
Send a private representation
The first fragment assumes an existing authenticated route and an application-defined public projection. It marks the response private before sending JSON. Express can generate an ETag for suitable responses under its configured settings; verify the actual deployed configuration rather than assuming a particular value. Private allows a private cache under HTTP semantics and is not the same as forbidding all storage. Use an appropriate no-store policy when storage itself is unacceptable.
app.get('/profile', requireUser, async (req, res) => {
const profile = await loadPublicProfile(req.user.id);
res.set('Cache-Control', 'private, no-cache');
res.json(profile);
});
The route assumes authorization and a safe projection. Verify ETag and cache settings in the deployed application.
Check a conditional request
The second fragment illustrates checking req.fresh after setting the status and a representation-specific ETag. The version must change whenever the selected representation changes, and the route must already have authorized the caller. A 304 response has no representation body. Do not use a single validator for different tenant-specific views unless it actually identifies each representation correctly; vary cache keys and policy for all relevant dimensions.
res.status(200).set('ETag', representationTag);
if (req.fresh) return res.status(304).end();
return res.json(representation);
The example assumes a correctly quoted validator and prior authorization. A real application must maintain representation-specific versions.
Consider invalidation and intermediaries
A changed record, permissions change or change in projected fields can alter what a client is allowed to see. Define how validators and cache policy reflect those changes, including language and content negotiation when relevant. Proxies and browsers may cache independently, so inspect actual response headers through the deployed path. A validator helps revalidation; it does not purge every previously cached copy automatically.
Test authorized and stale cases
Using synthetic records, request an authorized representation, repeat with the returned validator and check the conditional response, then update the record and verify the new representation is returned. Test a different user and a denied request, including any permission change. Inspect Cache-Control, ETag and Vary where applicable. An in-process test validates application headers but cannot prove a CDN or browser applied the intended policy.
Common errors and fixes
A 304 is returned before authorization
- Why it happens
- Freshness was checked before the resource access policy.
- How to fix it
- Authenticate and authorize the selected representation first.
- Prevent it next time
- Test a matching validator from a denied user.
A private response is shared across users
- Why it happens
- Cache policy or keys ignored the user-specific representation.
- How to fix it
- Use a suitable private or no-store policy and correct cache dimensions.
- Prevent it next time
- Test two synthetic users through the intended proxy.
A stale body remains valid after an update
- Why it happens
- The validator did not change with the representation.
- How to fix it
- Derive or update the validator for every relevant change.
- Prevent it next time
- Test data and permission changes with conditional requests.
2 practice exercises
1. A client sends a matching ETag but has lost access. Should it receive 304?
Show worked solution
The server must first enforce the current authorization policy. A matching validator is not permission. Test the denied response and ensure no private representation or metadata leaks.
2. Why does private differ from no-store?
Show worked solution
Private restricts reuse by shared caches but can permit storage in a private cache; no-store directs caches not to store the response. Select and test the policy appropriate to the sensitivity and deployment.
Key takeaways
- A validator represents content, not authorization.
- Verify private caching and change behavior across real intermediaries.