📚 Encyclopedia

Web Development · updated 2026-10-26

Caching static sites and unbundled ES modules (?v= stamps, no-cache, prefetch)

cachinges-modulescache-controlapacheview-transitionsprefetch

Justin's personal sites are static HTML plus plain JavaScript, often native ES modules with no bundler. Without a build step to fingerprint filenames, cache policy has to be chosen deliberately, or a deploy mixes stale and fresh files.

The problem with unbundled modules

A page imports main.js, which imports ten more modules. With no Cache-Control at all, browsers apply heuristic caching (a fraction of the time since Last-Modified), so after a deploy a returning visitor can get the new index.html with some old modules: broken imports or subtle state mismatches. Two fixes, used together:

  1. Version stamps on every reference you control. <script src="app.js?v=7">, importScripts('m-core.js?v=21'), new Worker('worker.js?v=21'). Bump N on every deploy that changes the file (each project's notes say which stamps exist). With a stamp, the response can be immutable. - Automatic variant (3d.jhampton.io): PHP writes ?v=<filemtime> and an Apache <If "%{QUERY_STRING} =~ /^v=/"> block marks such responses public, max-age=31536000, immutable.
  2. no-cache for module directories whose files are imported by bare paths (module-to-module imports cannot easily carry a stamp). no-cache means "revalidate every time" (ETag or Last-Modified, so a 304 costs one round trip), not "do not cache". On justinahampton.com this is a <Directory .../fun/arcade/joyride> Header set Cache-Control "no-cache" block per game, added for Joyride and Voyages; vendor libraries get immutable through a <Location>.

The dev-server variant of the same bug

python3 -m http.server sends Last-Modified with no Cache-Control, so the browser heuristically caches ES modules during development too, and a refresh runs yesterday's code. In the Spacebar JS rebuild this produced a "black screen" and an installEditor is not a function error: a fresh main.js dynamically imported a stale cached editor.js (mixed-version modules; reproduced exactly by serving the previous commit's file). Fixes: - A tiny Node static server (tools/serve.mjs) that sends Cache-Control: no-store for local dev. - Cache-bust dynamic imports with a per-page-load id: import(`./editor.js?v=${BOOT_ID}`). - In production (nginx-only static vhost), Cache-Control: no-cache so ETag revalidation returns cheap 304s while never serving a stale module. - Dev servers started from a shell die when the shell is cleaned up; check curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:PORT/ before assuming the code is broken.

Do not make CSS no-cache on page-to-page sites

On the 3d site, CSS served no-cache cost about a second of revalidation per navigation and the page flashed unstyled ("The site flashes with no css on page switching"). Fix: stamped immutable CSS plus the background color inlined in <head>.

Pages themselves

Cross-document view transitions and prefetch

A multi-page site can feel like an app without becoming a SPA (Justin asked; the recommendation was no SPA, for link previews, no double rendering, and a small site): - @view-transition { navigation: auto; } in CSS, with the header given a view-transition-name so it holds still, and turned off under prefers-reduced-motion. Chrome supports cross-document transitions; Firefox ignored the rule safely when tested. - Prefetch on hover, touchstart and focus for visitors only (gated by a data-prefetch attribute on <html>), skipping admin, login/logout, file downloads and streaming endpoints. - Tests that navigate mid-transition log "AbortError: Transition was skipped"; filter it.

Gzip

Unbundled JS is large. The justinahampton.com vhost ships ~270 KB of planet-generator JS uncompressed that would be ~85 KB deflated; gzip is opt-in per site on the Alma server (see Inline blur-up placeholders (LQIP): JPEG beats WebP across a page).

Apache merge order (why these rules win)

<Directory> sections apply first, then <Files>/<FilesMatch>, then <Location>, then <If>. That is how a <Location> vendor rule and an <If> query-string rule override a directory-level default. See Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects).

No flash on first load (fonts and reveal effects)

How oklahomafreightbrokerassociation.org reached Lighthouse 100 with zero layout shift:

See also

Linked from

Sources: spacebar memory (reference_dev_tooling, project_js_rebuild), justinahampton memory (3d-subdomain, joyride-architecture, experiments-voyages, freetime-planet, freetime-orrery, gbs-tutorial)