Skip to content

Commit f94a942

Browse files
authored
[web] Self-cleaning service worker (flutter#176834)
Introduce a self-cleaning service worker to replace the old one. Previous attempts (flutter#170918, flutter#173609) had (accidentally?) disabled the loading of the service worker in `flutter.js`. This PR preserves the logic for loading the service worker, and it prints a deprecation warning. Towards flutter#156910 Closes flutter#106225
1 parent 0c55239 commit f94a942

20 files changed

Lines changed: 205 additions & 1805 deletions

dev/bots/service_worker_test.dart

Lines changed: 125 additions & 753 deletions
Large diffs are not rendered by default.

dev/bots/suite_runners/run_web_tests.dart

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -197,48 +197,7 @@ class WebTestsSuite {
197197
() => _runGalleryE2eWebTest('debug'),
198198
() => _runGalleryE2eWebTest('profile'),
199199
() => _runGalleryE2eWebTest('release'),
200-
() =>
201-
runWebServiceWorkerTest(headless: true, testType: ServiceWorkerTestType.withoutFlutterJs),
202-
() => runWebServiceWorkerTest(headless: true, testType: ServiceWorkerTestType.withFlutterJs),
203-
() => runWebServiceWorkerTest(
204-
headless: true,
205-
testType: ServiceWorkerTestType.withFlutterJsShort,
206-
),
207-
() => runWebServiceWorkerTest(
208-
headless: true,
209-
testType: ServiceWorkerTestType.withFlutterJsEntrypointLoadedEvent,
210-
),
211-
() => runWebServiceWorkerTest(
212-
headless: true,
213-
testType: ServiceWorkerTestType.withFlutterJsTrustedTypesOn,
214-
),
215-
() => runWebServiceWorkerTest(
216-
headless: true,
217-
testType: ServiceWorkerTestType.withFlutterJsNonceOn,
218-
),
219-
() => runWebServiceWorkerTestWithCachingResources(
220-
headless: true,
221-
testType: ServiceWorkerTestType.withoutFlutterJs,
222-
),
223-
() => runWebServiceWorkerTestWithCachingResources(
224-
headless: true,
225-
testType: ServiceWorkerTestType.withFlutterJs,
226-
),
227-
() => runWebServiceWorkerTestWithCachingResources(
228-
headless: true,
229-
testType: ServiceWorkerTestType.withFlutterJsShort,
230-
),
231-
() => runWebServiceWorkerTestWithCachingResources(
232-
headless: true,
233-
testType: ServiceWorkerTestType.withFlutterJsEntrypointLoadedEvent,
234-
),
235-
() => runWebServiceWorkerTestWithCachingResources(
236-
headless: true,
237-
testType: ServiceWorkerTestType.withFlutterJsTrustedTypesOn,
238-
),
239-
() => runWebServiceWorkerTestWithGeneratedEntrypoint(headless: true),
240-
() => runWebServiceWorkerTestWithBlockedServiceWorkers(headless: true),
241-
() => runWebServiceWorkerTestWithCustomServiceWorkerVersion(headless: true),
200+
() => runServiceWorkerCleanupTest(headless: true),
242201
() => _runWebStackTraceTest('profile', 'lib/stack_trace.dart'),
243202
() => _runWebStackTraceTest('release', 'lib/stack_trace.dart'),
244203
() => _runWebStackTraceTest('profile', 'lib/framework_stack_trace.dart'),

dev/integration_tests/flutter_gallery/web/index.html

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -19,72 +19,6 @@
1919
<link rel="manifest" href="manifest.json">
2020
</head>
2121
<body>
22-
<!-- This script installs service_worker.js to provide PWA functionality to
23-
application. For more information, see:
24-
https://developers.google.com/web/fundamentals/primers/service-workers -->
25-
<script>
26-
var serviceWorkerVersion = null;
27-
var scriptLoaded = false;
28-
function loadMainDartJs() {
29-
if (scriptLoaded) {
30-
return;
31-
}
32-
scriptLoaded = true;
33-
var scriptTag = document.createElement('script');
34-
scriptTag.src = 'main.dart.js';
35-
scriptTag.type = 'application/javascript';
36-
document.body.append(scriptTag);
37-
}
38-
39-
if ('serviceWorker' in navigator) {
40-
// Service workers are supported. Use them.
41-
window.addEventListener('load', function () {
42-
// Wait for registration to finish before dropping the <script> tag.
43-
// Otherwise, the browser will load the script multiple times,
44-
// potentially different versions.
45-
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
46-
navigator.serviceWorker.register(serviceWorkerUrl)
47-
.then((reg) => {
48-
function waitForActivation(serviceWorker) {
49-
serviceWorker.addEventListener('statechange', () => {
50-
if (serviceWorker.state == 'activated') {
51-
console.log('Installed new service worker.');
52-
loadMainDartJs();
53-
}
54-
});
55-
}
56-
if (!reg.active && (reg.installing || reg.waiting)) {
57-
// No active web worker and we have installed or are installing
58-
// one for the first time. Simply wait for it to activate.
59-
waitForActivation(reg.installing ?? reg.waiting);
60-
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
61-
// When the app updates the serviceWorkerVersion changes, so we
62-
// need to ask the service worker to update.
63-
console.log('New service worker available.');
64-
reg.update();
65-
waitForActivation(reg.installing);
66-
} else {
67-
// Existing service worker is still good.
68-
console.log('Loading app from service worker.');
69-
loadMainDartJs();
70-
}
71-
});
72-
73-
// If service worker doesn't succeed in a reasonable amount of time,
74-
// fallback to plaint <script> tag.
75-
setTimeout(() => {
76-
if (!scriptLoaded) {
77-
console.warn(
78-
'Failed to load app from service worker. Falling back to plain <script> tag.',
79-
);
80-
loadMainDartJs();
81-
}
82-
}, 4000);
83-
});
84-
} else {
85-
// Service workers not supported. Just drop the <script> tag.
86-
loadMainDartJs();
87-
}
88-
</script>
22+
<script src="main.dart.js" type="application/javascript"></script>
8923
</body>
9024
</html>

dev/integration_tests/web/lib/service_worker_test_blocked_service_workers.dart

Lines changed: 0 additions & 13 deletions
This file was deleted.

dev/integration_tests/web/lib/service_worker_test_cached_resources.dart

Lines changed: 0 additions & 23 deletions
This file was deleted.

dev/integration_tests/web/web/index_with_blocked_service_workers.html

Lines changed: 0 additions & 50 deletions
This file was deleted.

dev/integration_tests/web/web/index_with_flutterjs.html

Lines changed: 0 additions & 41 deletions
This file was deleted.

dev/integration_tests/web/web/index_with_flutterjs_custom_sw_version.html

Lines changed: 0 additions & 41 deletions
This file was deleted.

dev/integration_tests/web/web/index_with_flutterjs_el_nonce.html

Lines changed: 0 additions & 51 deletions
This file was deleted.

dev/integration_tests/web/web/index_with_flutterjs_el_tt_on.html

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)