@@ -7,7 +7,29 @@ import { FlutterEntrypointLoader } from './entrypoint_loader.js';
77import { FlutterTrustedTypesPolicy } from './trusted_types.js' ;
88import { loadCanvasKit } from './canvaskit_loader.js' ;
99import { loadSkwasm } from './skwasm_loader.js' ;
10- import { getCanvaskitBaseUrl } from './utils.js' ;
10+ import { getCanvaskitBaseUrl , resolveUrlWithSegments } from './utils.js' ;
11+
12+ /**
13+ * Fire-and-forget cleanup for legacy `flutter_service_worker.js?v=` registrations
14+ * whose script URL may be cached indefinitely by a CDN.
15+ *
16+ * TODO(kevmoo): Remove in 2027 H2 (https://github.com/flutter/flutter/issues/156910).
17+ * @param {TrustedTypePolicy? } ttPolicy
18+ */
19+ function cleanupLegacyServiceWorker ( ttPolicy ) {
20+ if ( 'serviceWorker' in navigator ) {
21+ navigator . serviceWorker . getRegistration ( ) . then ( ( registration ) => {
22+ const scriptURL = registration ?. active ?. scriptURL ?? '' ;
23+ if ( scriptURL . includes ( 'flutter_service_worker.js?v=' ) ) {
24+ let url = resolveUrlWithSegments ( `flutter_service_worker.js?v=${ Date . now ( ) } ` ) ;
25+ if ( ttPolicy != null ) {
26+ url = ttPolicy . createScriptURL ( url ) ;
27+ }
28+ navigator . serviceWorker . register ( url ) ;
29+ }
30+ } ) ;
31+ }
32+ }
1133
1234/**
1335 * The public interface of _flutter.loader. Exposes two methods:
@@ -27,7 +49,7 @@ export class FlutterLoader {
2749 */
2850 async loadEntrypoint ( options ) {
2951 const { serviceWorker, ...entrypoint } = options || { } ;
30- if ( serviceWorker ) {
52+ if ( serviceWorker ?. serviceWorkerUrl ) {
3153 console . warn (
3254 'FlutterLoader.loadEntrypoint: The "serviceWorker" option is deprecated and ignored. ' +
3355 'Register custom service workers directly with navigator.serviceWorker.register(). ' +
@@ -36,6 +58,7 @@ export class FlutterLoader {
3658 }
3759 // A Trusted Types policy that is going to be used by the loader.
3860 const flutterTT = new FlutterTrustedTypesPolicy ( ) ;
61+ cleanupLegacyServiceWorker ( flutterTT . policy ) ;
3962 // The FlutterEntrypointLoader instance could be injected as a dependency
4063 // (and dynamically imported from a module if not present).
4164 const entrypointLoader = new FlutterEntrypointLoader ( ) ;
@@ -63,7 +86,7 @@ export class FlutterLoader {
6386 config,
6487 serviceWorkerSettings,
6588 } = { } ) {
66- if ( serviceWorkerSettings ) {
89+ if ( serviceWorkerSettings ?. serviceWorkerUrl ) {
6790 console . warn (
6891 'FlutterLoader.load: The "serviceWorkerSettings" option is deprecated and ignored. ' +
6992 'Register custom service workers directly with navigator.serviceWorker.register(). ' +
@@ -160,6 +183,7 @@ export class FlutterLoader {
160183
161184 const deps = { } ;
162185 deps . flutterTT = new FlutterTrustedTypesPolicy ( ) ;
186+ cleanupLegacyServiceWorker ( deps . flutterTT . policy ) ;
163187
164188 const canvasKitBaseUrl = getCanvaskitBaseUrl ( config , buildConfig ) ;
165189 if ( build . renderer === "canvaskit" ) {
0 commit comments