Senior front-end jobs at news sites list caching, CDNs and image optimization as three separate requirements. It's really one subject: deliver the page without redoing work that's already done. On a news site the same headline gets read millions of times, and rendering it, querying the database and reprocessing the photo on every read doesn't add up.
To write this article I checked my own site with curl -I and found hashed files going out with max-age=0. I opened the adapter's code and found out the mistake was in my test. But along the way a real problem showed up, in the images. I tell both stories further down. If LCPCore Web Vitals metric that measures how long the largest visible element on screen, usually an image or heading, takes to appear., INPCore Web Vitals metric that measures how long the page takes to visually react to clicks, taps and key presses during a visit. and CLSCore Web Vitals metric that measures how much content jumps around while the page loads, like a button that moves down when an image appears. are still new to you, start with the guide to Core Web Vitals and the Lighthouse score; this one picks up from there.
Four Caches Between the Reader and the Database
What you use every day stays in the pantry, one step away. When something runs out, you go to the supermarket, which is far. Caching is deciding what stays in the pantry, for how long, and how to tell the jar has expired.

- Browser. If the stored response is fresh, it doesn't even touch the network. It's the fastest cache and the only one you can't clear remotely.
- CDN edge. Servers close to readers. A HIT here saves the trip to your infrastructure.
- Origin shield. A middle layer that collects requests from several edges. On CloudFront it consolidates simultaneous requests for the same object into "as few as one request" to the origin, and it's available in the São Paulo region (AWS).
- Server and data. The already rendered page (ISRVariant of static generation where prebuilt pages are rebuilt in the background after a time limit or a trigger, without a new build., render cache) and, below it, Redis or the database. The most expensive stop. When each page is born is covered in choosing between SSR, SSG, ISR and CSR.
The rule that organizes everything comes from Harry Roberts: solve cache busting before you think about caching (CSS Wizardry, 2019, updated in 2025). If the URL changes along with the content, you can keep it forever. If it doesn't, you have to revalidate or purge.
Cache-Control Without Memorizing
The current spec is RFC 9111, from 2022 (IETF). Without Cache-Control, the browser may compute a TTL from Last-Modified (the RFC mentions 10% of the time since the last modification). Sending no header doesn't mean "don't cache".
| Directive | What it does in practice |
|---|---|
max-age=N |
Fresh for N seconds; doesn't even ask the server. |
s-maxage=N |
Same, for shared caches (CDN) only. There, it wins over max-age. |
no-cache |
Stores, but validates before every use (304 if nothing changed). |
no-store |
Doesn't store anywhere. |
immutable |
Won't change while fresh: no revalidation even on reload. |
stale-while-revalidate=N |
Serves the stale copy for up to N seconds and updates in the background. |
stale-if-error=N |
If the origin returns 500, 502, 503 or 504, serves the stale copy. |
private |
Only the browser stores it. |
Sources: MDN and, for the stale-* directives, RFC 5861.
stale-while-revalidate is the one that changes the experience the most. With max-age=1, stale-while-revalidate=59, during the first second the browser uses the cache; from second 1 to 60 it serves what it has and revalidates in the background; after that, it waits for the network (web.dev). The reader never waits for revalidation. Support goes back to Chrome 75 and Firefox 68, and browsers without it just apply max-age.
Vary changes the cache key: the headers it lists have to match those of the original request (RFC 9111, section 4.1). Vary: Accept-Encoding is harmless. Vary: Cookie on a public page practically turns the shared cache off.
Recipes Per Resource Type
| Resource | Header |
|---|---|
| JS, CSS and images with a hash or version in the URL | public, max-age=31536000, immutable |
| Image with a fixed name | public, max-age=86400, stale-while-revalidate=604800 |
| Article or home page HTML | public, max-age=60, stale-while-revalidate=600 or no-cache |
| Public read API | public, max-age=30, stale-while-revalidate=300 |
| Logged-in area, preview, dashboard | private, no-store |
| Sitemap and RSS | public, max-age=600, stale-while-revalidate=3600 |
web.dev boils it down to two rules: a versioned URL gets max-age=31536000; an unversioned URL gets no-cache (web.dev, Grigorik and Posnick). Jake Archibald explains why the middle ground is dangerous: max-age on content that changes "can appear to work in testing, but break stuff in the real world" (2016).
On my blog, the HTML goes out with public, max-age=60, stale-while-revalidate=600 (frontend/src/pages/blog/[slug].astro:22). If I fix a typo, it's gone within a minute, and nobody waits on the network to read. In Next.js, in an APIContract that defines how one program asks another for data or actions: which endpoints to call, what to send and what comes back. On the web, usually HTTP with JSON. route:
// app/api/headlines/route.ts
export async function GET() {
const headlines = await fetchHeadlines();
return Response.json(headlines, {
headers: { 'Cache-Control': 'public, max-age=30, stale-while-revalidate=300' },
});
}Invalidation Is the Hard Part: Purge by Tag
A story with a misspelled name in the headline shows up on the story page, the section page, the home page and the feed. Purging URL by URL doesn't scale.
CDNs solve this with labels: Cache-Tag on Cloudflare, Surrogate-Key on Fastly. The origin sends the tags along with the page, and when you fix something you ask "purge everything tagged story-123".
HTTP/1.1 200 OK
Cache-Control: public, max-age=0, must-revalidate
CDN-Cache-Control: max-age=600, stale-while-revalidate=60, stale-if-error=86400
Cache-Tag: story-123, section-politics, homeCloudflare Cache-Tag |
Fastly Surrogate-Key |
|
|---|---|---|
| Header limit | 16 KB, about 1,000 tags | 16 KB; each key up to 1 KB |
| Case | Case-insensitive | Case-sensitive |
| When exceeded | The per-response cap applies | The key being read and all after it are ignored |
Sources: Cloudflare, 2026 and Fastly. The Fastly row is where incidents come from: too many tags and the last ones vanish silently. Put the story's own tag first.
In Next.js, revalidateTag and revalidatePath purge the application cache. The docs warn that with several instances, each one has its own disk cache, and revalidation only clears the one that received the call (Next.js, 2026). With three replicas, the reader sees the fixed version on one reload and the wrong one on the next. The way out is a shared cache handler, in Redis for example.
CDN: Hit Ratio, Origin Shield and a Header Just for the Edge
A newsstand is a CDN: the truck drops off the bundle once, and the neighborhood picks up a ready copy on the corner.

According to the 2025 Web Almanac, 35% of mobile HTML goes through a CDN, and 71% among the top 1,000 sites. Cloudflare serves 58% of that HTML, Google 21% and CloudFront 7% (HTTP Archive, 2026).
- Cache hit ratio is the share of requests the edge answered without going to the origin. Look at it by request and by bytes, separately: a high hit rate on icons can hide MISSes on the landing page, which is the one that weighs on LCP. To raise it: strip
utm_*from the key and avoidVary: Cookie. - Origin shield turns the dozens of simultaneous MISSes for a story that goes viral into a single request. On CloudFront it's billed separately.
CDN-Cache-Control(RFC 9213, from 2022) gives the CDN its own TTL; a CDN that reads this header ignoresCache-Control(IETF). Since only the edge can be purged, the browser gets a TTL of zero and the edge gets ten minutes, as in the example above. Check whether your CDN reads this header; if not,s-maxagedoes a similar job.
Large news sites usually run more than one CDN. Globo, Brazil's largest media group, added Magalu Cloud to the providers behind its CDN, which has about 300 points of presence in Brazil, as part of a multicloud strategy (Baguete, 2025). The price is that purging becomes one call per provider, and the cache key has to be equivalent across all of them.
Images: The LCP Is Almost Always a Photo
The LCP element is an image on 76% of mobile pages and 85% of desktop pages (HTTP Archive, 2026).
| Format | Global support (caniuse, 11/19/2026) | When to use |
|---|---|---|
| AVIF | 96.71% | First choice for photos. Smaller, slower to encode. |
| WebP | 97.32% | Safe fallback. The next/image default. |
| JPEG XL | 69.25% | Not yet. It arrives in Chrome 155; the number comes from Safari. |
| JPEG | universal | Last fallback inside <picture>. |
Sources: caniuse for AVIF, WebP and JPEG XL. Chrome had removed JPEG XL in 2022, and the October 2026 announcement says decoding comes back "starting in Chrome 155", with a decoder written in Rust (Chrome for Developers, 2026). As I write this, 155 isn't stable; on Android it starts in 157, in Firefox in 158. Anyone saying "Chrome already supports it" is ahead of themselves.
The 1,600 px photo at the top of my site weighs 179,471 bytes as JPG, 98,782 as WebP and 39,974 as AVIF: the AVIF is 78% smaller than the JPG and 60% smaller than the WebP. You can test the quality in Squoosh before deciding.
The size has to be right too. srcset lists the versions and sizes says how much of the screen the image takes up; the 2024 Almanac found 14% of sizes attributes wrong enough to change which version got downloaded (HTTP Archive, 2024).
<picture>
<source type="image/avif" srcset="/img/cover-640.avif 640w, /img/cover-1280.avif 1280w" sizes="(min-width: 1024px) 720px, 100vw" />
<img src="/img/cover-1280.webp" srcset="/img/cover-640.webp 640w, /img/cover-1280.webp 1280w"
sizes="(min-width: 1024px) 720px, 100vw" width="1280" height="720"
alt="Description of the scene" fetchpriority="high" />
</picture>The Top Image Doesn't Wait
web.dev is blunt: don't lazy load images that are visible on load, "especially LCP images" (web.dev). Even so, 16% to 17% of pages do it. And only 17.3% of mobile pages give the LCP image fetchpriority="high"; on Google Flights, that change took LCP from 2.6 s to 1.9 s (web.dev).
Just don't mark everything as a priority. Nuvemshop, the Latin American e-commerce platform that took its share of pages with good LCP from 57% to 96%, puts it this way: "if everything is high priority, nothing is" (web.dev, 2026). They also widened edge caching without caching prices or stock.
If the LCP image is a CSS background, the browser only finds it after the CSS; the fix is preload. That's my case: Head.astro:162 preloads the AVIF with a 1x and 2x imagesrcset and fetchpriority="high", and the comment on lines 152 to 155 records the earlier bug, where the width-based version downloaded two photos.
Next/image and Its Limits
According to the docs (2026):
- The default format is WebP only. AVIF takes about 50% longer to encode and comes out around 20% smaller; with both formats, the cache doubles.
- Optimized versions stay cached for at least 4 hours, and "there is no mechanism to invalidate". Changed the photo? Change the file name.
- Behind a CDN, it has to forward the
Acceptheader, or the optimizer won't know the browser accepts AVIF. - In Next 16,
qualitiesis required andprioritywas deprecated in favor ofpreloadorloading="eager"withfetchPriority="high".
At scale, the server's own optimizer becomes a CPU bottleneck, and news sites use a dedicated image service. web.dev estimates 40% to 80% savings in file size with an image CDN (web.dev). One of the best-known open source ones was born in Brazil: Thumbor, from globo.com, which resizes and crops on demand.
What I Found on My Own Site
My portfolio runs on Astro 7 with the standalone Node adapter. On a hashed CSS file:
$ curl -I https://ramongomessilva.com.br/_astro/FooterIsland.BqKFdZJp.css
cache-control: public, max-age=0
etag: W/"1e205-1a1e1250858"
A hashed file revalidating on every visit? I went to read the adapter. It sets public, max-age=31536000, immutable on everything under /_astro/, but only on the stream event of the library that serves the file. On a HEAD request, which is what curl -I sends, there's no body, the event never fires and the header doesn't show up. With GET:
$ curl -s -D - -o /dev/null https://ramongomessilva.com.br/_astro/FooterIsland.BqKFdZJp.css
cache-control: public, max-age=31536000, immutable
First lesson: check caching with GET.
The real problem was right next to it. Whatever lives in public/, without a hash, goes out with public, max-age=0 and a weak ETag even with GET: the logo and the top photos. Every repeat visit asks the server just to hear "304, not modified". With a fixed name, the server can't know whether the file will change; I even take advantage of that to swap blog images instantly.
I fixed it like this, on the day I published this article:
- Images, fonts and logos in
public/now get a moderate TTL:public, max-age=86400, stale-while-revalidate=604800. Astro's adapter doesn't let you configure the header for these files, so I put a few-line server in front of it (frontend/server/cache-publico.mjs). It sets the TTL before passing the request on, and the library that serves the file respects a header it finds already set. If the file doesn't exist, the response goes out withno-store, so a 404 isn't kept for a day. Swapping a blog photo now takes up to a day for returning visitors, which is fine for a blog. - Layout images (logo, top photo) can still move to Astro's image component. The build generates hashed names under
/_astro/, which already getimmutable. That's the next step, less urgent now that they have a one-day TTL. - HTML and CMS media stay as they are: the HTML already has
stale-while-revalidate, and media already goes out with one year andimmutable(media-public.route.v1.ts:24, with an integration test).
On Ofertume, my deals site built on Next 16, the picture is similar. /_next/static/ goes out with immutable, as it should. The guide photos, in public/img/ with a 640 and 1280 srcset, go out with max-age=0: the same gap. The prerendered home page goes out with s-maxage=31536000: a CDN in front of it would keep the HTML for a year, and purging would be mandatory on every publish. And Vary lists five internal App Router headers (rsc, next-router-state-tree and others), which multiply the cache keys. It's the framework's default, visible only in the raw response.
A News Site Case Study
Terra, a Brazilian portal with 63 million unique visitors a month, explained on web.dev how it sped up moving between stories: the next one started being fetched in the background, with a low-priority fetch, IntersectionObserver and requestIdleCallback, and only on 3G or better and with 4 GB of RAM or more. They did it by hand because Safari supported neither rel=prefetch nor Speculation Rules. Result: LCP 51% lower on mobile and 73% on desktop, and ad clicks up 11% and 30% (web.dev). The story was already in the browser when the reader clicked, and the network and memory filter avoids stealing bandwidth from weak devices.
If I were setting up caching for a news site today:
| Layer | Configuration |
|---|---|
| Story HTML | Browser max-age=0, must-revalidate; edge 5 to 10 min with stale-while-revalidate and a one-day stale-if-error; tags per story, section and author. |
| Home and section pages | Edge 30 to 60 s with stale-while-revalidate. |
| JS, CSS, fonts | Hash in the name, immutable for a year. |
| Photos | Image service with AVIF and WebP, versioned URL; first photo with fetchpriority="high", the rest lazy. |
| Origin | Origin shield and a cache handler shared across replicas. |
stale-if-error is your insurance for election night: if the origin goes down at the peak, the edge keeps serving the last good version. And no no-store on public HTML. 23% of sites use it, and it takes the page out of the bfcache, the back/forward cache; one in five mobile navigations is a back or forward (web.dev).
How to Measure Whether It Worked
- Headers, with GET.
cf-cache-status,x-cache,ageandx-nextjs-cache. Ifageis always zero, the edge isn't storing anything. - CrUX. Chrome field data, a 28-day rolling average with about two days of lag (Chrome for Developers); Chrome on iOS is left out (methodology). PageSpeed Insights shows CrUX data for any public URL.
- RUM with
web-vitals. The attribution build costs about 1.5 KB more and tells you the why behind each number (GitHub):
import { onLCP } from 'web-vitals/attribution';
onLCP(({ value, attribution }) => {
navigator.sendBeacon('/api/vitals', JSON.stringify({
lcp: value,
element: attribution.target,
ttfb: attribution.timeToFirstByte,
loadDelay: attribution.resourceLoadDelay,
loadDuration: attribution.resourceLoadDuration,
}));
});If ttfb dominates, the problem is HTML caching or a slow origin. If loadDelay dominates, the image is discovered late (lazy, CSS, JS). If loadDuration dominates, it's heavy or far away. For frame-by-frame synthetic testing, use WebPageTest.
When to Use Each Layer
- Browser only: a small site without a CDN, with hashed static files and
stale-while-revalidateon the HTML. That's my portfolio today. - CDN: audience far from the server, traffic spikes, content read many times. For a news site, mandatory. Even with the server in Brazil, median mobile DNS time drops from 217 ms to 176 ms with a CDN, according to the Almanac.
- Image service: lots of new photos every day. With half a dozen fixed ones, generating AVIF and WebP at build time is enough.
Pitfalls
- Testing caching with
curl -I: HEAD can return different headers than GET. - Long
max-ageon an unversioned URL: the reader gets stuck on the old version. no-storeby default: it kills the bfcache and the shared cache.Vary: Cookieor a bloatedVary: every combination becomes a key and the hit ratio plummets.- Lazy loading the top image, or
fetchpriority="high"on all of them. srcsetwithout the rightsizes: the browser downloads the biggest version.- A purge that only clears one replica in self-hosted Next.
If your site is slow and you want a diagnosis of its caching, CDN and images with fixes ranked by priority, that's what I do in website speed optimization.
Frequently Asked Questions
What's the difference between no-cache, no-store and max-age=0?
no-store forbids storing. no-cache stores, but validates before every use, and max-age=0 makes the response born stale, which also leads to validation. On a public page, the last two keep the bfcache; no-store doesn't.
Does stale-while-revalidate work in the browser or only on the CDN?
Both. In the browser, since Chrome 75 and Firefox 68; browsers without support just apply max-age. On the CDN, it depends on the provider.
How do I invalidate the CDN cache when I publish a story?
Tag responses per story and section (Cache-Tag or Surrogate-Key) and call the tag purge when you publish. Keep the browser TTL short, since you can't clear it.
AVIF or WebP in 2026?
AVIF first, with WebP as a fallback in <picture> or through negotiation on the image CDN. Both are above 96% support; AVIF is usually much smaller for photos, but slower to encode.
fetchpriority or preload for the LCP image?
If it's an <img> in the HTML, fetchpriority="high" is enough. If it's a CSS background or comes in through JavaScript, use preload with fetchpriority="high".
References
- IETF, RFC 9111: HTTP Caching, 2022.
- IETF, RFC 9213: Targeted HTTP Cache Control, 2022.
- IETF, RFC 5861: HTTP Cache-Control Extensions for Stale Content, 2010.
- MDN, Cache-Control, 2026, and HTTP caching, 2025.
- Ilya Grigorik and Jeff Posnick, HTTP Cache, web.dev, accessed in 2026.
- Jeff Posnick, Keeping things fresh with stale-while-revalidate, web.dev, accessed in 2026.
- Harry Roberts, Cache-Control for Civilians, 2019, updated in 2025.
- Jake Archibald, Caching best practices & max-age gotchas, 2016.
- Cloudflare, Purge cache by cache-tags, 2026.
- Fastly, Working with surrogate keys, accessed in 2026.
- AWS, CloudFront Origin Shield, accessed in 2026.
- Next.js, Incremental Static Regeneration and Image component, 2026.
- HTTP Archive, Web Almanac 2025, CDN and Performance, 2026; Media, 2024.
- Chrome for Developers, JPEG XL in Chrome, 2026, and 103 Early Hints, 2022, updated in 2026.
- web.dev, Fetch Priority, image lazy loading, image CDNs and bfcache, accessed in 2026.
- Chrome for Developers, CrUX API and CrUX methodology, accessed in 2026.
- GoogleChrome, web-vitals library, version 5, 2026.
- caniuse, AVIF, WebP and JPEG XL, accessed on 11/19/2026.
- Guilherme Moser de Souza and Thierno Thiam, Terra: prefetching stories, web.dev, accessed in 2026.
- web.dev, Nuvemshop: Core Web Vitals, 2026.
- Maurício Renner, Globo coloca Magalu Cloud no seu CDN, Baguete, 2025 (in Portuguese).
- globo.com, Thumbor, open source, accessed in 2026.
- Book: Ilya Grigorik, High Performance Browser Networking, O'Reilly, 2013. Free to read at hpbn.co. Still the best explanation of why every network round trip is expensive.
- Book: Addy Osmani, Image Optimization, Smashing Magazine, 2021.
- Book: Barry Pollard, HTTP/2 in Action, Manning, 2019.