Lesson 7 of 14 · 24m
Caching and Delivery: Making the Second Visit Free
Two-tier Cache-Control for fingerprinted assets vs. HTML, edge-cached HTML to kill distant TTFB, connection warmup, and the bfcache.
I watched a site's repeat-visit LCP get worse after a CDN migration. The new edge config respected the origin's Cache-Control: no-store on everything — including fingerprinted JS and fonts — so every navigation re-downloaded 600 KB that hadn't changed in weeks. Caching is the cheapest performance win you will ever ship, and the easiest to break without noticing.
The two-tier strategy
Nearly every site caches correctly in exactly two ways:
- Fingerprinted assets (
index-3f9a1c.js,inter-var-8b2e.woff2): the filename changes when the content does, so the URL can live in cache forever. - HTML and unversioned URLs: must revalidate, so a deploy is visible immediately.
# /etc/nginx/conf.d/shop.conf
location ~* ^/assets/.+-[0-9a-f]{6,}[.](js|css|woff2|avif|webp|svg)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location / {
# store it, but check with the server before every use (cheap 304 via ETag)
add_header Cache-Control "no-cache";
etag on;
try_files $uri /index.html;
}immutable tells the browser not to even revalidate on reload. no-cache is badly named: it means "store, but revalidate," not "don't cache." no-store is the one that means never store — reserve it for genuinely sensitive responses.
Compression is part of delivery
Check that text assets ship compressed: content-encoding: br (Brotli) on JS, CSS, and HTML beats gzip by ~15–20%, and zstd is the newer option Chrome already accepts. Precompress at build time (brotli -k dist/assets/*.js or your bundler's compression plugin) so the server doesn't pay CPU per request. Never compress what's already compressed — JPEG, AVIF, WOFF2, and MP4 gains nothing and can grow.
Let the CDN absorb TTFB
TTFB is the first LCP sub-part, and for users far from your origin it's mostly round trips. Caching HTML at the edge — even briefly — collapses it:
Cache-Control: public, max-age=0, s-maxage=60, stale-while-revalidate=600Browsers always revalidate (max-age=0); the CDN serves its copy for 60 s (s-maxage), and for ten minutes after that it can serve stale instantly while fetching fresh in the background. For product and article pages this is routinely the difference between an 1,800 ms and a 150 ms TTFB in distant regions. Personalized HTML needs a different shape — cache the shell, fetch the personal bits.
Connection setup is hidden latency
Every new origin costs DNS + TCP + TLS — easily 300–600 ms on mobile before a byte moves. Cut origins on the critical path (self-host fonts, serve images from your main domain or CDN) and warm up the ones you can't remove:
<link rel="preconnect" href="https://img.northwindgear-cdn.com" crossorigin>
<link rel="dns-prefetch" href="https://analytics.northwindgear.com">Preconnect to two or three origins you'll definitely use early — each costs a socket and CPU. Servers and CDNs that support 103 Early Hints can send these hints (and preloads) while the HTML is still being generated, reclaiming server think time.
The back/forward cache
The bfcache keeps a full page snapshot in memory, so Back and Forward restore it instantly — effectively zero LCP. Pages lose eligibility by registering unload handlers, holding certain connections open, or being served Cache-Control: no-store. DevTools → Application → Back/forward cache has a Test button that lists exactly what blocked you.
Field notes
- Verify with real headers:
curl -sI https://shop.northwindgear.com/assets/index-3f9a1c.js | grep -iE 'cache-control|content-encoding|age'. The config you think is deployed is not always the config that's deployed. - A climbing
ageheader andx-cache: HITmean edge caching works; constantMISSusually means aSet-CookieorVary: Cookieis busting it. - Service workers can make repeat visits instant — and can pin users to a broken build for weeks. Treat SW caching as a deployment system with a rollback plan.
- Segment RUM by
navigationType:back-forward/back-forward-cacheloads should have near-zero LCP; if they never appear, something is blocking eligibility.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
PHP API Performance: N+1 Queries, cURL, and Connection Pools
A PHP API case study: fixing an N+1 query, twenty serial cURL calls, missing connection pooling, and stats recalculated on every request with Redis caching.
Inside TypeScript 7: What a Year-Long Rewrite in Go Actually Bought Developers
A deep look at how Microsoft's Go-based TypeScript 7 compiler achieves its speed gains, and what the Visual Studio Code team's year-long migration reveals about adopting it safely.
Node.js vs Deno vs Bun: Picking the Right JavaScript Runtime in 2026
Three JavaScript runtimes. Three different philosophies on what the developer experience should feel like. Here's how they compare on performance, ecosystem, tooling, and the only decision that actually matters for production.