The fastest request is the one that never leaves the device, and the second fastest is the one answered from a server a few milliseconds away. Caching and content delivery networks are the two mechanisms that make both happen. For a gaming site, whose assets are large and change rarely, they are the highest-leverage infrastructure decisions available. This guide explains the layers, the HTTP headers that control them, and the versioning discipline that makes aggressive caching safe.
Three layers of cache
Every request passes through up to three caches before reaching the server that generates the content.
- Browser cache. Stored on the user's device. A hit costs no network at all. Governed by the
Cache-Controlheader the response carried when it was first fetched. - Edge cache (CDN). A content delivery network runs servers, called points of presence or edges, in many locations. The user's request is routed to the nearest one. If the edge holds a fresh copy of the asset, it answers directly; the round trip is short because the edge is close. If not, the edge fetches from the origin once and then serves every subsequent user from its copy.
- Origin. The application server or storage that holds the authoritative content. The slowest and farthest layer, and the one you want to reach as rarely as possible.
The CDN also terminates TLS near the user, so the expensive handshakes in the latency pipeline happen over a short distance, and it typically keeps warm connections to the origin so cache misses are cheaper than a cold fetch would be. web.dev's CDN article covers these benefits and the configuration choices in more depth.
Cache-Control
HTTP caching is specified in RFC 9111 and documented on MDN. The Cache-Control response header carries directives that every cache along the path honours:
| Directive | Meaning | Use for |
|---|---|---|
max-age=N | fresh for N seconds after the response was generated | everything; the core directive |
s-maxage=N | like max-age, but only for shared caches such as CDNs | HTML that the edge may hold longer than the browser |
public / private | may be stored by shared caches, or only by the browser | private for personalised responses |
immutable | will never change during its freshness lifetime; skip revalidation even on reload | hashed asset URLs |
no-cache | may be stored but must be revalidated before every reuse | HTML that must always be current but can be served as 304 |
no-store | must not be stored at all | sensitive responses |
stale-while-revalidate=N | may serve a stale copy for N seconds while fetching a fresh one in the background | edge caching of HTML and APIs |
A response with no Cache-Control is not uncached; caches apply a heuristic, commonly a fraction of the time since Last-Modified, which MDN warns is unpredictable. Set the header explicitly on every response.
Freshness and revalidation
A cached response is fresh while its age is below max-age, and the cache serves it without contacting anyone. Once it is stale, the cache may still use it, but only after asking the origin whether it has changed. That question is a conditional request: the cache sends If-None-Match with the ETag it holds (or If-Modified-Since with the date). If the content is unchanged the origin answers 304 Not Modified with headers only, and the cache reuses its stored body. If it changed, the origin sends a full 200 with the new body. A 304 still costs a round trip, which is why static assets are better served with a long max-age and immutable, so the revalidation never happens.
Immutable assets and cache busting
Long cache lifetimes are only safe if a changed file gets a new URL, because a browser holding game.js for a year will not notice the server's new version. The solution is to put a content hash in the file name, such as game.3f9a1c2e.js, and reference the new name from the HTML whenever the content changes. The old URL stays valid for anyone with an old HTML page; the new URL is a cache miss once and then cached for a year. Google's documentation notes that Googlebot may ignore caching headers, which makes fingerprinted file names the reliable way to ensure it fetches updated assets too.
Query-string versioning (site.css?v=8d0ac4e4) is the lighter alternative and is what this site uses: the HTML references each stylesheet and script with a hash of its content, and the server sends a one-year immutable policy for those file types. It works with every browser cache; its weakness is that a few CDNs and proxies ignore query strings, so hashed file names are the more robust choice for a large deployment.
HTML itself cannot be fingerprinted, since its URL is the page's address. Give it a short freshness (minutes), or no-cache with an ETag so the browser revalidates cheaply, and let the CDN hold it for longer with s-maxage and stale-while-revalidate so most users get an edge hit.
Compression: Brotli and gzip
The client lists the encodings it understands in Accept-Encoding; the server picks one, compresses the body, and names it in Content-Encoding. Both algorithms in common use are supported by all current browsers. gzip is universal and fast. Brotli (br, RFC 7932) usually compresses text 15 to 25 percent smaller than gzip at comparable settings, with slower compression at its highest levels; the standard approach is to pre-compress static files at build time with Brotli at a high level and gzip as a fallback, and to compress dynamic HTML on the fly at a moderate level. Apply compression only to text types (HTML, CSS, JavaScript, SVG, JSON, XML); image, font and audio formats are already compressed.
Because the same URL now has several representations, the response must carry Vary: Accept-Encoding so that caches store the Brotli and gzip versions separately and never hand a Brotli body to a client that cannot decode it.
A caching policy for a gaming site
| Resource | URL strategy | Cache-Control |
|---|---|---|
| HTML pages | stable URL | public, max-age=600, s-maxage=3600, stale-while-revalidate=86400 |
| CSS, JavaScript, game runtime | content hash in name or query | public, max-age=31536000, immutable |
| Texture atlases, audio | content hash in name | public, max-age=31536000, immutable |
| Images (page) | hashed if built; otherwise stable | hashed: immutable; stable: max-age=2592000 |
| API responses (public, non-personal) | stable | public, max-age=0, s-maxage=60, stale-while-revalidate=300 |
| Personalised or session data | stable | private, no-store |
The values are a reasonable starting point rather than a rule; a site with a release every hour wants shorter HTML freshness than one that changes weekly.
Service workers
A service worker is a script the browser runs in the background with its own programmable cache. It can pre-cache a game's runtime and first-scene assets on first visit, serve them instantly on return visits even when offline, and apply policies HTTP headers cannot express, such as serving a cached atlas immediately while checking for a new one. The cost is complexity and the risk of serving stale content indefinitely if the update logic is wrong. For a gaming site it is worth considering once the HTTP caching above is in place, not instead of it.
Verifying
Open the browser's network panel and look at the Cache-Control, Content-Encoding, Vary and ETag headers on a page, a stylesheet and an atlas. Reload and confirm the hashed assets come from the memory or disk cache with no request. Check that the CDN reports a cache status header (most expose one) and that a second request for an asset from the same edge is a hit. Confirm the HTML is compressed and that the server sends a real 304 for a conditional request on a stale page. None of this requires tooling beyond the browser.
Sources
Primary documentation consulted for this guide. Thresholds and API behaviour were checked against these pages on 2026-11-02.
- HTTP caching — MDN Web Docs
- Cache-Control header — MDN Web Docs
- ETag header — MDN Web Docs
- Vary header — MDN Web Docs
- RFC 9111: HTTP Caching — IETF RFC Editor
- RFC 9110: HTTP Semantics — IETF RFC Editor
- Prevent unnecessary network requests with the HTTP Cache — web.dev
- Content delivery networks (CDNs) — web.dev
- Compression in HTTP — MDN Web Docs
- RFC 7932: Brotli Compressed Data Format — IETF RFC Editor
- Service Worker API — MDN Web Docs
- Optimize Time to First Byte — web.dev