Skip to content

fix: Serve immutable files from the service worker cache first - #1096

Merged
MartinZikmund merged 13 commits into
mainfrom
dev/mazi/pwa-cache-first
Oct 9, 2026
Merged

MartinZikmund merged 13 commits into
mainfrom
dev/mazi/pwa-cache-first

Conversation

@MartinZikmund

Copy link
Copy Markdown
Member

Fixes #1092

What changed

The PWA service worker was network-first for every request, including the files that never change. It fell back to its cache only when fetch() rejected before the response headers arrived, so a connection reset mid-body reached the app even with everything cached. It also registered right after runMain, and its install downloaded every offline file during the app's first render (for an Uno app, ~60 font files and every icon and splash scale: about 5.5 MB of brotli).

  • Cache-first for package_<hash> files (except uno-config.js) and fingerprinted _framework files; other requests are network-first with a 4 s timeout. The whole body is read inside that window, so a reset falls back to the cache instead of truncating index.html.
  • Runtime caching: files the app fetches are cached as it uses them.
  • Install only caches what startup needs (the boot resources, package_* JS/CSS, uno-config.js, index.html). The rest of offline_files is cached in the background, three at a time, when the page posts an uno-precache message. This isn't done in activate: fetch events wait for activation to complete, so a reload would have waited for the whole precache.
  • Registration waits for the load event, 3 s, and an idle period.
  • WasmShellPWAPrecacheExclude (semicolon-separated globs, * and **) leaves files out of the background caching. They're still cached on use. The default excludes nothing, so offline behavior is unchanged.
  • Docs: "Offline support" section on the PWA page.

Validation

New Uno.Wasm.Tests.ServiceWorker (CI, linux). It loads the RayTracer to install the worker, checks the precache, then reloads with the server offline, then with every file's first request reset mid-body.

  • Before: offline reload OK, flaky reload fails.
  • After: both start, in ~1.1 s on the test server.
  • The RayTracer now sets WasmShellPWAPrecacheExclude=pwa-images/**. Of its 103 images, 0 are precached and every other published offline file is.

Measured with a prototype of this worker on a Uno 7.0 template app, through a test server simulating 4G (repeat visits, median, n=3): warm reload 2.41 → 1.55 s, offline 3.39 → 1.79 s, warm reload on a flaky network 2/3 → 3/3 started.

Found while testing, not changed here: offline_files lists WebWorker files with unexpanded #[.{fingerprint}] placeholders, and the worker's dotnet.js under a stale fingerprint. Those 404, so a WebWorker app can't start its worker offline. The test skips these entries.

🤖 Generated with Claude Code

https://claude.ai/code/session_01N5azy6DU7ujLte16VPVYQs

Copilot AI balanced review requested due to automatic review settings October 1, 2026 11:42
@MartinZikmund MartinZikmund self-assigned this Oct 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Runtime cache writes are not lifetime-tracked, tests can produce false positives, and the required feature specification is missing.

Review effort: Balanced
Findings: 4 Medium severity · 1 Low severity

Open (5)
What changed in this PR

Improves PWA reliability and startup performance by introducing cache-first immutable assets, deferred precaching, and runtime caching.

Changes:

  • Adds configurable background-precache exclusions and delayed service-worker registration.
  • Reworks service-worker caching and network-failure handling.
  • Adds documentation, unit tests, and browser-based CI validation.
File Description
src/​Uno.Wasm.Tests.ServiceWorker/​server.js Adds offline/flaky test server.
src/​Uno.Wasm.Tests.ServiceWorker/​run-test.sh Runs service-worker tests.
src/​Uno.Wasm.Tests.ServiceWorker/​package.json Defines browser-test dependency.
src/​Uno.Wasm.Tests.ServiceWorker/​package-lock.json Locks test dependencies.
src/​Uno.Wasm.Tests.ServiceWorker/​app.js Tests precaching and offline reloads.
src/​Uno.Wasm.Sample.RayTracer/​Uno.Wasm.Sample.RayTracer.csproj Configures image precache exclusion.
src/​Uno.Wasm.Bootstrap/​ts/​Uno/​WebAssembly/​Bootstrapper.ts Defers registration and initiates precaching.
src/​Uno.Wasm.Bootstrap/​ShellTask.cs Emits exclusion configuration.
src/​Uno.Wasm.Bootstrap/​JsStringHelper.cs Serializes semicolon-separated values.
src/​Uno.Wasm.Bootstrap/​Embedded/​service-worker.js Implements new caching strategies.
src/​Uno.Wasm.Bootstrap/​build/​Uno.Wasm.Bootstrap.targets Passes the MSBuild property.
src/​Uno.Wasm.Bootstrap.UnitTests/​Given_JsStringHelper_ToJsStringArray.cs Tests configuration serialization.
doc/​features-pwa.md Documents offline behavior and exclusions.
build/​ci/​stage-build-linux-tests.yml Adds service-worker CI coverage.
Files not reviewed (1)
  • src/Uno.Wasm.Tests.ServiceWorker/package-lock.json: Generated file

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Uno.Wasm.Tests.ServiceWorker/app.js Resolved
Comment thread src/Uno.Wasm.Tests.ServiceWorker/app.js Resolved
Copilot AI balanced review requested due to automatic review settings October 2, 2026 06:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Runtime caching can expose dynamic responses, while retry and concurrent-precache paths have correctness and performance defects.

Review effort: Balanced
Findings: 1 High severity · 3 Medium severity

Open (4)
Resolved since last review (5)
Files not reviewed (1)
  • src/Uno.Wasm.Tests.ServiceWorker/package-lock.json: Generated file

Copilot AI balanced review requested due to automatic review settings October 2, 2026 06:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Response reconstruction breaks valid HTTP semantics, while unrestricted runtime caching can persist sensitive API responses.

Review effort: Balanced
Findings: 1 High severity · 3 Medium severity

Open (4)
Files not reviewed (1)
  • src/Uno.Wasm.Tests.ServiceWorker/package-lock.json: Generated file

Copilot AI balanced review requested due to automatic review settings October 2, 2026 14:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Runtime caching can expose incorrect API data, and valid or retried responses are not handled consistently.

Review effort: Balanced
Findings: 1 High severity · 3 Medium severity

Open (4)
Files not reviewed (1)
  • src/Uno.Wasm.Tests.ServiceWorker/package-lock.json: Generated file
Previously missed (1)

In code that hasn't changed since last review

Medium severity Successful retries are not stored in the runtime cache

src/​Uno.Wasm.Bootstrap/​Embedded/​service-worker.js:172

A successful retry is returned without being added to the runtime cache. Consequently, an excluded asset whose first attempt exceeds the four-second window loads on the retry but is not available offline, contradicting the cached-on-use behavior. Track successful retry responses with the same event.waitUntil(putInCache(...)) path as the initial attempt.

Copilot AI balanced review requested due to automatic review settings October 8, 2026 08:24
@MartinZikmund
MartinZikmund force-pushed the dev/mazi/pwa-cache-first branch from d617363 to cd268ca Compare October 8, 2026 08:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI balanced review requested due to automatic review settings October 8, 2026 09:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI balanced review requested due to automatic review settings October 8, 2026 09:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

MartinZikmund and others added 9 commits October 8, 2026 13:40
The service worker was network-first for every request and only fell back
to its cache when fetch() rejected before the response headers arrived. A
connection dropping mid-body reached the app even with every file cached, so
a repeat visit on a flaky network could fail, and every file waited on the
network first. It also registered right after runMain, so its install-time
download of every offline file (fonts and icons included) competed with the
app's first render.

- package_<hash> files and fingerprinted _framework files are served from
  the cache first; other requests are network-first with a 4 s timeout,
  reading the whole body before answering so a reset falls back to the cache
- files the app fetches are cached as it uses them
- install caches only what boot needs; the remaining offline files are
  cached in the background, a few at a time, on a message from the page
  (not in 'activate', which holds back fetches until it completes)
- registration waits for the load event, a few seconds and an idle period
- WasmShellPWAPrecacheExclude leaves files out of the background caching

Measured on a simulated 4G link: repeat visits 36% faster (2.41 s ->
1.55 s), offline startup 3.4 s -> 1.8 s, and warm loads on a flaky
network no longer fail.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N5azy6DU7ujLte16VPVYQs
After the first visit installs the service worker, the RayTracer must start
again with the server offline, then with every file's first request reset
mid-body. Also checks that WasmShellPWAPrecacheExclude keeps the sample's
pwa-images out of the background caching.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N5azy6DU7ujLte16VPVYQs
uno-config.js now sits next to index.html, so the package folder is fully
immutable and the install precaches the config from the root.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01ADrzrqtw3RQdHan77rypBM
MartinZikmund and others added 2 commits October 8, 2026 13:40
Every same-origin GET went through the 4s network-first path, so slow API
calls and streams were cut off, and their responses were cached and could
be replayed for a different query. Other requests now go to the browser
untouched, and no-store responses aren't cached.

Also reads retried responses fully, keeps null-body statuses bodiless, and
shares one background precache across tabs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01ADrzrqtw3RQdHan77rypBM
A response slower than the network timeout must arrive whole, and must
not be cached.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01ADrzrqtw3RQdHan77rypBM
Copilot AI balanced review requested due to automatic review settings October 8, 2026 11:41
@MartinZikmund
MartinZikmund force-pushed the dev/mazi/pwa-cache-first branch from eadbd05 to f01d614 Compare October 8, 2026 11:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@mergify

mergify Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

Copilot AI balanced review requested due to automatic review settings October 8, 2026 15:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@MartinZikmund
MartinZikmund merged commit 3e29e01 into main Oct 9, 2026
16 of 17 checks passed
@MartinZikmund
MartinZikmund deleted the dev/mazi/pwa-cache-first branch October 9, 2026 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Service worker: cached files still wait on the network, and the precache competes with startup

3 participants