Public: anyone can read this; the crew can change it.

/sw.js is revalidated on every load: a new release's service worker is never held for four hours #14036

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

I changed /sw.js to send cache-control: private, no-cache instead of a bare no-cache. It now also answers a matching If-None-Match with a 304. I haven't confirmed that the edge stops rewriting the header, because no preview of this branch existed while I worked.

Notes

shipped production is serving it deploy 4adf612

opened this

I changed /sw.js to send cache-control: private, no-cache instead of a bare no-cache. It now also answers a matching If-None-Match with a 304. I haven't confirmed that the edge stops rewriting the header, because no preview of this branch existed while I worked.

Cause. The origin was already sending no-cache, and a test already checked for it. On production the edge rewrote that to max-age=14400, even on a cache MISS and with a cache-busting query string. That 4-hour value is the edge's default browser cache time, and it gets applied to cacheable extensions like .js. Other cacheable routes on production show the same thing: /og/audit.svg and a 404 on a .csv path both come out as max-age=14400. The one I found sent through unchanged was /audit.csv, whose private, no-store arrived as is (BYPASS). That is the reason for private, which also stops the edge from keeping a shared copy of the worker.

What changed

  • src/web/sw.ts: SW_CACHE_CONTROL = "private, no-cache". A matching If-None-Match now gets a 304 with no body and the same headers (etag, service-worker-allowed: /, the worker's CSP). The match accepts the edge's weakened W/"…" form as well as the strong tag. Before, the ETag was sent but never answered with a 304. The FORGE_SW_KILL worker still has no ETag. The route stays where it was in src/http/server.ts; only its comment changed.
  • test/offline.test.ts:
    • asserts the new header and that it has no max-age;
    • checks the 304 for a strong tag, a weak tag and a list of tags, and a full 200 for a different tag;
    • asserts that the hashed stylesheet under /assets/ keeps public, max-age=31536000, immutable.
  • docs/FEED.md: one line updated.

Not done

  • The issue asked for exactly no-cache. A bare no-cache is what production already got and then lost at the edge, so I added private. It still means "revalidate every time", just with no shared copy.
  • I didn't change the edge's own settings: they aren't held in this repository.
  • private passing through untouched is inferred from the /audit.csv example (private, no-store). No private, no-cache response on a cacheable extension was found on production to check against, so the preview curl under "How to try it" settles it.

Checks. bun x tsc --noEmit, bun scripts/gate.ts and bun scripts/design-lint.ts are clean. I ran 7 test files: offline, offline-page, smoke, release-smoke, release-prove, no-polling and route-marks; 146 passed. scripts/fast.ts named 2362 test files, too many to run here, so the forge's full check covers the rest. The new 304 test would fail on the old code, which never sent a 304.

How to try it. On the preview, run curl -I <preview>/sw.js and look for cache-control: private, no-cache with no max-age=14400. Repeat it with -H 'if-none-match: <the etag>' to get a 304.

🤖 Generated with Claude Code

Closes #13883

commented

I changed /sw.js to send cache-control: private, no-cache instead of a bare no-cache. It now also answers a matching If-None-Match with a 304. I haven't confirmed that the edge stops rewriting the header, because no preview of this branch existed while I worked.

Cause. The origin was already sending no-cache, and a test already checked for it. On production the edge rewrote that to max-age=14400, even on a cache MISS and with a cache-busting query string. That 4-hour value is the edge's default browser cache time, and it gets applied to cacheable extensions like .js. Other cacheable routes on production show the same thing: /og/audit.svg and a 404 on a .csv path both come out as max-age=14400. The one I found sent through unchanged was /audit.csv, whose private, no-store arrived as is (BYPASS). That is the reason for private, which also stops the edge from keeping a shared copy of the worker.

What changed

  • src/web/sw.ts: SW_CACHE_CONTROL = "private, no-cache". A matching If-None-Match now gets a 304 with no body and the same headers (etag, service-worker-allowed: /, the worker's CSP). The match accepts the edge's weakened W/"…" form as well as the strong tag. Before, the ETag was sent but never answered with a 304. The FORGE_SW_KILL worker still has no ETag. The route stays where it was in src/http/server.ts; only its comment changed.
  • test/offline.test.ts:
    • asserts the new header and that it has no max-age;
    • checks the 304 for a strong tag, a weak tag and a list of tags, and a full 200 for a different tag;
    • asserts that the hashed stylesheet under /assets/ keeps public, max-age=31536000, immutable.
  • docs/FEED.md: one line updated.

Not done

  • The issue asked for exactly no-cache. A bare no-cache is what production already got and then lost at the edge, so I added private. It still means "revalidate every time", just with no shared copy.
  • I didn't change the edge's own settings: they aren't held in this repository.
  • private passing through untouched is inferred from the /audit.csv example (private, no-store). No private, no-cache response on a cacheable extension was found on production to check against, so the preview curl under "How to try it" settles it.

Checks. bun x tsc --noEmit, bun scripts/gate.ts and bun scripts/design-lint.ts are clean. I ran 7 test files: offline, offline-page, smoke, release-smoke, release-prove, no-polling and route-marks; 146 passed. scripts/fast.ts named 2362 test files, too many to run here, so the forge's full check covers the rest. The new 304 test would fail on the old code, which never sent a 304.

How to try it. On the preview, run curl -I <preview>/sw.js and look for cache-control: private, no-cache with no max-age=14400. Repeat it with -H 'if-none-match: <the etag>' to get a 304.

🤖 Generated with Claude Code

Approved

Clean. The preview answers /sw.js with cache-control: private, no-cache and no max-age=14400 (cf-cache-status BYPASS), keeps service-worker-allowed, and ETag matching now gives a 304 (weak compare handles the edge weakening). Tests cover both headers, the 304 variants and the immutable asset cache. No new input surface, no dependency. Note: private, no-cache rather than bare no-cache, justified by the edge rewrite and verified on the preview.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs