Svelte / Advanced: rendering, resources and release evidence / Lesson 14
HTTP caching, freshness and bounded source work
Coordinate response audience, load invalidation and source deadlines without inventing a universal framework cache.
On this page
Lesson goals
- Distinguish HTTP caches, client load state and application caches.
- Define source budgets, stale-fill policy and private response eligibility.
Name which representation is reused
A browser HTTP cache, CDN, load result and application data cache have different keys and lifetimes. Kit load invalidation does not automatically purge every external layer, and a fresh load can receive an old source-cache value. Define whether a representation is public, tenant-specific or principal-specific before sharing it. A URL-only key is not enough for every personalized response. Cache presence also does not establish current permission. Distinguish source absence, source failure and cache miss internally, even if some public outcomes intentionally look similar under the disclosure policy. The actual adapter and headers determine the deployment’s cache behavior.
Cache only an explicitly public endpoint representation
The endpoint fragment returns a validated public course list with a synthetic sixty-second public max-age. It is not a recommendation for sessions, account data or permission decisions. HTTP intermediaries apply their own documented rules, so verify audience and Vary requirements for the actual deployment. The helper and endpoint imports are assumed and unexecuted. A successful response does not prove persistent storage or backup. Keep mutation results and authenticated representations out of this shared policy unless a separate reviewed contract explicitly permits the required keying and freshness behavior.
export async function GET(){
const courses=await catalog.validatedPublicList();
return Response.json({courses},{headers:{"cache-control":"public, max-age=60"}});
}
The endpoint is not started and the lifetime is a lab choice. No browser/CDN cache behavior is measured.
Expiry and invalidation do not impose transaction order
An old in-flight source read can refill stale data after a writer invalidates an entry. A shorter TTL limits duration under policy but does not remove that interleaving. Use source revisions or generation eligibility where required, and do not negative-cache an outage as legitimate absence. A title mutation can affect lists, sort order and metadata as well as one card. Track those semantic dependencies instead of assuming invalidateAll on the current page refreshes every representation everywhere. Keep cache keys free of raw credentials and document units and store behavior when integrating a real cache.
Reserve time for finalization before starting source work
The local arithmetic subtracts elapsed work and a finalization reserve from a synthetic total budget, clamping at zero. It does not configure a driver, interrupt a query or close a connection. A real adapter must honor the remaining budget and propagate supported cancellation; a client timeout alone can leave source work running. The example’s values are teaching numbers, not universal latency targets. Bound input cardinality, concurrency and payload size in addition to elapsed time. An unlimited Promise.all over a list can overwhelm a pool before any await rejects.
function remaining(total,elapsed,reserve){return Math.max(0,total-elapsed-reserve);}
console.assert(remaining(100,30,10)===60);
console.assert(remaining(100,95,10)===0);
console.assert(remaining(100,120,10)===0);
The budgets are sixty, zero and zero in the same synthetic unit. No deadline or cancellation is enforced.
Test fallback pressure and source correctness together
A cache outage can create many source misses at once. Bound refill concurrency and retries and choose one retry owner so client, server and source layers do not multiply attempts. Serve-stale policy must respect the domain: an old descriptive title is not an old revoked permission. Verify freshness, private-data isolation, source load and cleanup together using safe synthetic fixtures. This course executes only the budget function; HTTP cache headers, CDN state, invalidation, source cancellation and load/performance behavior remain unexecuted. Do not claim a benchmark or cache certification from a local calculation. Real integration must use the intended adapter and authorized environment.
Common errors and fixes
An uncached handler is assumed to return fresh source data
- Why it happens
- Response and source cache layers were conflated.
- How to fix it
- Identify and verify freshness at each actual layer.
- Prevent it next time
- Retain source-revision and response-cache fixtures separately.
A shared public header exposes private data
- Why it happens
- Response audience was not reviewed before enabling shared reuse.
- How to fix it
- Correct data projection and private/shared cache policy together.
- Prevent it next time
- Test different principals using the same URL.
Layered retries multiply source pressure
- Why it happens
- Several layers independently owned retry attempts.
- How to fix it
- Choose a bounded retry owner and remaining-deadline policy.
- Prevent it next time
- Count attempts per logical operation in isolated tests.
Check your understanding
1. Does invalidateAll purge a CDN or application cache automatically?
Show worked solution
No. It operates under Kit’s documented load/navigation contract. External HTTP or domain caches have separate policies and interfaces. Identify the stale representation, verify the authoritative source revision and repair the relevant layer rather than repeating a broad UI refresh until the result changes by chance.
2. Why does the remaining-budget function not prove source cancellation?
Show worked solution
It computes numbers only. The source adapter must receive and enforce a supported deadline or cancellation signal, and resources may still remain active after the caller stops waiting. Verify actual interruption and cleanup separately, especially when a write may already have committed.
Key takeaways
- HTTP and domain cache layers require explicit audience and freshness.
- Source deadlines, concurrency and replay policy must agree.