-
Notifications
You must be signed in to change notification settings - Fork 29.7k
feat: Remove service worker #174991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Remove service worker #174991
Conversation
There was a problem hiding this comment.
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 service worker functionality from Flutter's web builds. The existing service worker, which handled caching, is replaced by a new, simple "cleanup" worker. This new worker's only purpose is to delete old caches and unregister itself, ensuring a clean state for users with a previously cached application. The changes span flutter_tools to generate this new worker, flutter.js to remove the service worker registration logic, and the test suite to verify the cleanup process.
My review focuses on the new test implementation, where I've found a critical issue in the test-specific service worker content that would cause the test to fail, and a medium-severity issue regarding redundant file operations in the test setup. The rest of the changes appear correct and effectively achieve the goal of removing the service worker.
| const String cleanupWorkerContent = ''' | ||
| 'use strict'; | ||
| const CACHE_NAME = 'flutter-app-cache'; | ||
| self.addEventListener('install', () => { | ||
| self.skipWaiting(); | ||
| console.log('Deprecated service worker installed. It will not be used.'); | ||
| }); | ||
| // remove old caches and unregister the service worker | ||
| self.addEventListener('activate', (event) => { | ||
| event.waitUntil( | ||
| (async () => { | ||
| try { | ||
| const deletePromises = OLD_CACHE_NAMES.map((key) => self.caches.delete(key)); | ||
| await Promise.all(deletePromises); | ||
| } catch (e) { | ||
| console.warn('Failed to delete old service worker caches:', e); | ||
| } | ||
| Future<void> _generateEntrypoint() async { | ||
| final Directory tempDirectory = Directory.systemTemp.createTempSync( | ||
| 'flutter_web_generated_entrypoint.', | ||
| ); | ||
| await runCommand(_flutter, <String>[ | ||
| 'create', | ||
| 'generated_entrypoint_test', | ||
| ], workingDirectory: tempDirectory.path); | ||
| final File generatedEntrypoint = File( | ||
| path.join(tempDirectory.path, 'generated_entrypoint_test', 'web', 'index.html'), | ||
| ); | ||
| final String generatedEntrypointCode = generatedEntrypoint.readAsStringSync(); | ||
| final File testEntrypoint = File( | ||
| path.join( | ||
| _testAppWebDirectory, | ||
| _testTypeToIndexFile(ServiceWorkerTestType.generatedEntrypoint), | ||
| ), | ||
| ); | ||
| testEntrypoint.writeAsStringSync(generatedEntrypointCode); | ||
| tempDirectory.deleteSync(recursive: true); | ||
| } | ||
| try { | ||
| await self.registration.unregister(); | ||
| } catch (e) { | ||
| console.warn('Failed to unregister service worker:', e); | ||
| } | ||
| Future<void> _setAppVersion(int version) async { | ||
| final File targetFile = File(_targetPath); | ||
| await targetFile.writeAsString( | ||
| (await targetFile.readAsString()).replaceFirst( | ||
| RegExp(r'CLOSE\?version=\d+'), | ||
| 'CLOSE?version=$version', | ||
| ), | ||
| try { | ||
| const clients = await self.clients.matchAll({ | ||
| type: 'window', | ||
| includeUncontrolled: true, | ||
| }); | ||
| // Reload clients to ensure they are not using the old service worker. | ||
| clients.forEach((client) => { | ||
| if (client.url && 'navigate' in client) { | ||
| client.navigate(client.url); | ||
| } | ||
| }); | ||
| } catch (e) { | ||
| console.warn('Failed to navigate service worker clients:', e); | ||
| } | ||
| })() | ||
| ); | ||
| } | ||
| }); | ||
| '''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cleanupWorkerContent constant is incorrect and will cause the test to fail. It's missing the definition for OLD_CACHE_NAMES (which it references) and contains an unused CACHE_NAME constant from the old service worker. The content should match the new service worker template to accurately test the cleanup scenario.
const String cleanupWorkerContent = '''
'use strict';
const OLD_CACHE_NAMES = ['flutter-app-manifest', 'flutter-app-cache', 'flutter-temp-cache'];
self.addEventListener('install', () => {
self.skipWaiting();
console.log('Deprecated service worker installed. It will not be used.');
});
// remove old caches and unregister the service worker
self.addEventListener('activate', (event) => {
event.waitUntil(
(async () => {
try {
const deletePromises = OLD_CACHE_NAMES.map((key) => self.caches.delete(key));
await Promise.all(deletePromises);
} catch (e) {
console.warn('Failed to delete old service worker caches:', e);
}
try {
await self.registration.unregister();
} catch (e) {
console.warn('Failed to unregister service worker:', e);
}
try {
const clients = await self.clients.matchAll({
type: 'window',
includeUncontrolled: true,
});
// Reload clients to ensure they are not using the old service worker.
clients.forEach((client) => {
if (client.url && 'navigate' in client) {
client.navigate(client.url);
}
});
} catch (e) {
console.warn('Failed to navigate service worker clients:', e);
}
})()
);
});
''';| ); | ||
|
|
||
| try { | ||
| cleanupWorkerFile.writeAsStringSync(cleanupWorkerContent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line writes cleanupWorkerContent to a file in the source directory. However, the flutter build web command on the next lines generates its own flutter_service_worker.js and does not use this file. This operation, along with the definition of cleanupWorkerFile (lines 33-38) and its deletion in the finally block (lines 162-164), is redundant. Consider removing these parts to simplify the test.
|
Marking as draft for now so we can investigate why the checks are not passing. |
|
This pull request has been changed to a draft. The currently pending flutter-gold status will not be able to resolve until a new commit is pushed or the change is marked ready for review again. For more guidance, visit Writing a golden file test for Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
|
This is being worked on here: #176834 |
Create a service worker that removes the old cached service worker.
Related issue: 156910
Pre-launch Checklist
///).