Fix Service Worker downloads 404ing in Firefox (small files) - #7
Merged
Merged
Conversation
…404ed
In Firefox the page can finish streaming a small staged file (port 'end')
before the iframe navigation reaches the Service Worker fetch handler,
which then answered 404 'Transfer not found or expired.' because the
entry had just been deleted. Chromium wins that race, so e2e never saw it.
- sw.js: keep the transfer entry after 'end' (stream is closed but fully
buffered, so a late fetch still serves the complete file); guard
re-serving with a 'served' flag; GC still reclaims old entries.
- sink.js: register the worker with updateViaCache:'none' so SW update
checks are never served from the HTTP cache (stale worker persisted
across deploys/containers otherwise).
- tests: makeSwBridge({ fetchAfterEnd }) reproduces Firefox's ordering;
new regression round-trip fails on the old sw.js, plus full suite.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Small-file downloads via the Service Worker sink returned 404 (Transfer not found or expired.) in Firefox, while the UI still reported success. Root cause is a race between the page and the SW, ported unchanged from FileSync.
Root cause
deliver()registers the transfer, navigates a hidden iframe to/__download/{id}, then drains the staged OPFS file through the MessageChannel and finishes with{ type: 'end' }— whichsw.jshandled bytransfers.delete(id)immediately.In Firefox, for small files, the OPFS drain (milliseconds) beats the iframe navigation's dispatch: the
fetchevent arrives afterenddeleted the entry, so the SW's own handler answered 404. Chromium dispatches faster, so the e2e suite (Chromium-only) never caught it. Verified experimentally: 256 KB → 404, 64 MB → 200 in real Firefox; same latent code exists in FileSync'sweb/sw.js(large transfers mask it there).Changes
src/sw.js: keep the transfer entry afterend— the stream is closed but fully buffered, so a late-arriving fetch still serves the complete file. Added aservedflag so a transfer can't be served twice (previously re-serving a locked stream would throw); the 5-minute GC still reclaims old entries.src/js/sink.js: register the worker withupdateViaCache: 'none'so SW update checks are never served from the HTTP cache — without this, a fixed worker can stay invisible across deploys/containers (observed live during testing: the old worker persisted even after the container was replaced).test/fake-sinks.mjs:makeSwBridge({ fetchAfterEnd })dispatches the simulated iframe fetch afterendis processed, reproducing Firefox's ordering deterministically in Node.test/sinks.test.mjs: regression round-trip using that mode (fails against the oldsw.js, passes now).Verification
npm test: 49/49 pass (new regression test fails on old code — checked via stash)sw/fs/bloball SHA-256 verified