From 73652a723bc6b40f5ecee7edaad89edd67c49b5c Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 16 Sep 2020 17:16:58 -0700 Subject: [PATCH] [flutter_tools] automatically update to latest sw on install (#65784) If a new service worker is installed, automatically update this behind the scenes. Immediately after a page refresh, the new worker and cache will be available, though the main.dart.js and others will be available sooner due to the cache busting URLS --- .../lib/src/build_system/targets/web.dart | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/build_system/targets/web.dart b/packages/flutter_tools/lib/src/build_system/targets/web.dart index 773eb808e9b..cdf866b4d45 100644 --- a/packages/flutter_tools/lib/src/build_system/targets/web.dart +++ b/packages/flutter_tools/lib/src/build_system/targets/web.dart @@ -476,6 +476,7 @@ const CORE = [ ${coreBundle.map((String file) => '"$file"').join(',\n')}]; // During install, the TEMP cache is populated with the application shell files. self.addEventListener("install", (event) => { + skipWaiting(); return event.waitUntil( caches.open(TEMP).then((cache) => { return cache.addAll( @@ -579,10 +580,12 @@ self.addEventListener('message', (event) => { // SkipWaiting can be used to immediately activate a waiting service worker. // This will also require a page refresh triggered by the main worker. if (event.data === 'skipWaiting') { - return self.skipWaiting(); + skipWaiting(); + return; } - if (event.message === 'downloadOffline') { + if (event.data === 'downloadOffline') { downloadOffline(); + return; } }); @@ -628,5 +631,11 @@ function onlineFirst(event) { }) ); } + +function skipWaiting() { + if (self.skipWaiting != undefined) { + self.skipWaiting(); + } +} '''; }