Skip to content

[web] Remove service worker loader and --pwa-strategy - #194785

Merged
auto-submit[bot] merged 14 commits into
flutter:masterfrom
kevmoo:issue-156910
Oct 29, 2026
Merged

auto-submit[bot] merged 14 commits into
flutter:masterfrom
kevmoo:issue-156910

Conversation

@kevmoo

@kevmoo kevmoo commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Removes FlutterServiceWorkerLoader from flutter.js, removes default serviceWorkerSettings injection from flutter_bootstrap.js, and fails --pwa-strategy with a migration error via throwToolExit.

  • Why build/web/flutter_service_worker.js is retained as a static cleanup stub: In Flutter <= 3.38 (the last stable series with an active offline-first caching service worker), flutter_service_worker.js serves flutter_bootstrap.js, flutter.js, and main.dart.js 100% cache-first from flutter-app-cache. Returning browsers upgrading directly from a <= 3.38 deployment only escape flutter-app-cache when the browser's W3C Service Worker Update check fetches /flutter_service_worker.js on navigation. If /flutter_service_worker.js returns 404 Not Found (or 200 text/html on SPA hosts), the W3C update check aborts and leaves the <= 3.38 worker active indefinitely.
  • WebServiceWorker.build continues emitting the self-unregistering build/web/flutter_service_worker.js stub (only when the project does not provide a custom web/flutter_service_worker.js), and the stub now also deletes the legacy 'flutter-app-manifest', 'flutter-temp-cache', and 'flutter-app-cache' Cache Storage buckets during activate.
  • WebTemplate.withSubstitutions logs a deprecation warning and replaces {{flutter_service_worker_version}} with null (tracked for 2027 H2 removal) so custom web/flutter_bootstrap.js or web/index.html files referencing the token do not fail with a JS SyntaxError.

Fixes #156910
Fixes #136620

Removes the deprecated `flutter_service_worker.js` self-destructing stub,
the hidden `--pwa-strategy` CLI option, default `serviceWorkerSettings`
bootstrap injection, and `FlutterServiceWorkerLoader` from `flutter.js`.

- `WebTemplate.withSubstitutions` continues replacing
  `{{flutter_service_worker_version}}` with `null` and a deprecation
  comment (tracked for 2027 H2 removal) so custom `web/flutter_bootstrap.js`
  files referencing the placeholder do not fail with a JS `SyntaxError`.
- `WebServiceWorker.build` continues emitting `precache_manifest.json`
  when `--web-content-hash` is enabled and writes an empty `service_worker.d`
  depfile when disabled so incremental builds do not re-trigger on a
  missing depfile.

Fixes flutter#156910
@github-actions github-actions Bot added tool Affects the "flutter" command-line tool. See also t: labels. engine flutter/engine related. See also e: labels. platform-web Web applications specifically team-web Owned by Web platform team labels Oct 19, 2026
…wa-strategy

- Keep emitting the static self-unregistering `build/web/flutter_service_worker.js` stub (unless the project provides a custom `web/flutter_service_worker.js`) and delete legacy `flutter-app-cache`, `flutter-temp-cache`, and `flutter-app-manifest` caches on `activate` so returning browsers upgrading directly from Flutter <= 3.38 cleanly unregister instead of hitting a W3C Service Worker Update 404 trap.
- Keep `--pwa-strategy` as a hidden option descriptor and fail with a targeted `throwToolExit` migration message in `flutter build web`.
@kevmoo kevmoo changed the title [web] Remove flutter_service_worker.js stub and --pwa-strategy [web] Remove service worker loader and --pwa-strategy Oct 19, 2026
@kevmoo
kevmoo marked this pull request as ready for review October 19, 2026 20:02
@flutter-dashboard

Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If both you and the PR author believe this PR qualifies for a test exemption, escalate to the tech lead for the changed area to review and apply the test-exempt label. All reviewers should feel empowered to ask for tests before escalating.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request removes the deprecated offline caching service worker from Flutter Web, deleting the service worker loader, removing the --pwa-strategy build option, and replacing the default service worker with a self-unregistering cleanup stub to purge legacy caches. Feedback on the changes suggests replacing the non-standard null-aware list element syntax (?) with standard collection-if elements in web.dart to ensure compatibility and maintainability.

@kevmoo
kevmoo requested a review from mdebbar October 19, 2026 20:09
@mdebbar

mdebbar commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

One thing I want to flag about dropping FlutterServiceWorkerLoader entirely.

In 3.41–3.47, the loader called getRegistration() and, if a registration existed, re-registered flutter_service_worker.js?v=<new random>. That was an active check with a fresh URL, so it always went to the network and picked up the cleanup stub.

With this PR, apps upgrading from <= 3.38 rely only on the browser's soft update on navigation, which refetches the old URL (flutter_service_worker.js?v=<old>). The browser bypasses its own HTTP cache for that fetch, but a CDN that caches by full URL (long max-age on *.js, no purge on deploy) will keep serving the old caching worker for ?v=<old>. Byte-identical script means no install, so the old worker stays active until the CDN entry expires. That's the exact wedged-app scenario the stub is meant to prevent.

Can we keep a minimal version of the active check in flutter.js? No settings, no public API, and it becomes a no-op once the old worker is gone:

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.getRegistration().then((registration) => {
    const scriptURL = registration?.active?.scriptURL ?? '';
    if (scriptURL.includes('flutter_service_worker.js?v=')) {
      navigator.serviceWorker.register(
        resolveUrlWithSegments(`flutter_service_worker.js?v=${Date.now()}`),
      );
    }
  });
}

registration.update() would be simpler, but it refetches the same ?v=<old> URL so it doesn't help with the CDN case. The ?v= check also keeps us from touching custom service workers that were registered directly.

Also, as far as I can tell the soft-update path was only verified on Chrome. The explicit register() doesn't depend on each browser's update scheduling.

mdebbar
mdebbar previously approved these changes Oct 19, 2026

@mdebbar mdebbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

#136620 can be closed now, right?

@kevmoo

kevmoo commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in b04bc47: narrowed the runtime warnings to ?.serviceWorkerUrl, removed the redundant serviceWorkerVersion = null substitution, added the fire-and-forget cleanupLegacyServiceWorker CDN cache-buster in loader.js, and linked #136620.

@kevmoo
kevmoo requested a review from mdebbar October 19, 2026 23:23
@kevmoo

kevmoo commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Can we keep a minimal version of the active check in flutter.js? No settings, no public API, and it becomes a no-op once the old worker is gone:

Added cleanupLegacyServiceWorker (with Trusted Types support via ttPolicy) in loader.js (b04bc47b4c8) so both loadEntrypoint and load re-register flutter_service_worker.js?v=${Date.now()} when an active registration matches flutter_service_worker.js?v=.

#136620 can be closed now, right?

Yes — added Fixes https://github.com/flutter/flutter/issues/136620 to the PR description so it auto-closes on merge.

@kevmoo
kevmoo requested a review from mdebbar October 20, 2026 18:29
mdebbar
mdebbar previously approved these changes Oct 21, 2026
@kevmoo
kevmoo requested a review from mdebbar October 21, 2026 16:51
mdebbar
mdebbar previously approved these changes Oct 27, 2026
@kevmoo

kevmoo commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Updated compile_web_test.dart for the 3-argument WebBuilder.buildWeb signature after merging master (34c791d); re-requesting review.

@kevmoo
kevmoo requested a review from mdebbar October 27, 2026 22:15
@mdebbar mdebbar added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 28, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 28, 2026
@auto-submit

auto-submit Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/194785, because - This pull request has the missing-tests label and does not have the test-exempt label. Please add tests or obtain a test exemption from a tech lead before re-applying the autosubmit label.

@mdebbar mdebbar added test-exempt Exempt from tests. Overrides `missing-tests` label autosubmit Merge PR when tree becomes green via auto submit App labels Oct 28, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Oct 29, 2026
Merged via the queue into flutter:master with commit 047cf8e Oct 29, 2026
34 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 29, 2026
@kevmoo
kevmoo deleted the issue-156910 branch October 29, 2026 17:10
johnpryan added a commit to flutter/devtools that referenced this pull request Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD engine flutter/engine related. See also e: labels. missing-tests platform-web Web applications specifically team-web Owned by Web platform team test-exempt Exempt from tests. Overrides `missing-tests` label tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[web] Deprecate and remove flutter_service_worker.js [web]Disappearance of flutter_service_worker.js content after flutter run

2 participants