Skip to content

Commit b04bc47

Browse files
committed
[web] Address review feedback: narrow serviceWorkerUrl warning and add legacy SW cache-buster
1 parent 245cfae commit b04bc47

3 files changed

Lines changed: 28 additions & 17 deletions

File tree

‎engine/src/flutter/lib/web_ui/flutter_js/src/loader.js‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,29 @@ import { FlutterEntrypointLoader } from './entrypoint_loader.js';
77
import { FlutterTrustedTypesPolicy } from './trusted_types.js';
88
import { loadCanvasKit } from './canvaskit_loader.js';
99
import { 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") {

‎packages/flutter_tools/lib/src/web_template.dart‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,6 @@ class WebTemplate {
136136
newContent = newContent.replaceAll(kStaticAssetsUrlPlaceholder, staticAssetsUrl);
137137
}
138138

139-
newContent = newContent.replaceFirst(
140-
// Support older `var` syntax as well as new `const` syntax
141-
RegExp('(const|var) serviceWorkerVersion = null'),
142-
'const serviceWorkerVersion = null /* $_kServiceWorkerDeprecationNotice */',
143-
);
144139
newContent = _applyVariableSubstitutions(newContent, logger, <String, String>{
145140
...webDefines,
146141
'flutter_build_config': ?buildConfig,

‎packages/flutter_tools/test/general.shard/web_template_test.dart‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ String htmlSample2Replaced({required String baseHref}) =>
198198
<div></div>
199199
<script src="main.dart.js"></script>
200200
<script>
201-
const serviceWorkerVersion = null /* Flutter's service worker is deprecated and has been removed. */;
201+
const serviceWorkerVersion = null;
202202
</script>
203203
<script>
204204
navigator.serviceWorker.register('flutter_service_worker.js');
@@ -321,14 +321,6 @@ void main() {
321321
);
322322
});
323323

324-
test('applies substitutions with legacy var version syntax', () {
325-
const indexHtml = WebTemplate(htmlSampleLegacyVar);
326-
expect(
327-
indexHtml.withSubstitutions(baseHref: '/foo/333/', flutterJsFile: flutterJs, logger: logger),
328-
htmlSample2Replaced(baseHref: '/foo/333/'),
329-
);
330-
});
331-
332324
test('applies substitutions to inline flutter.js bootstrap script', () {
333325
const indexHtml = WebTemplate(htmlSampleInlineFlutterJsBootstrap);
334326
final List<WebTemplateWarning> warnings = indexHtml.getWarnings();

0 commit comments

Comments
 (0)