From 84aae22c3eb0b0b7c37333ae0d23769e34534b84 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 17 Sep 2020 17:05:46 -0700 Subject: [PATCH] [flutter_tools] optimize fetch requests and remove main.dart.js bypass (#66069) The main.dart.js bypass is not needed now that we have skipWaiting. Additionally optimize the fetch handler so that resources not in the cache skip the service worker altogether. Fixes #66068 --- .../lib/src/build_system/targets/web.dart | 28 ++++++++----------- .../build_system/targets/web_test.dart | 4 +-- 2 files changed, 13 insertions(+), 19 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 cdf866b4d4..84b8b360f0 100644 --- a/packages/flutter_tools/lib/src/build_system/targets/web.dart +++ b/packages/flutter_tools/lib/src/build_system/targets/web.dart @@ -334,16 +334,12 @@ class WebReleaseBundle extends Target { outputFile.parent.createSync(recursive: true); } outputResourcesFiles.add(outputFile); - // insert a random hash into the requests for main.dart.js and service_worker.js. This is - // not a content hash, because it would need to be the hash for the entire bundle and not - // just the resource in question. + // insert a random hash into the requests for service_worker.js. This is not a content hash, + // because it would need to be the hash for the entire bundle and not just the resource + // in question. if (environment.fileSystem.path.basename(inputFile.path) == 'index.html') { final String randomHash = Random().nextInt(4294967296).toString(); final String resultString = inputFile.readAsStringSync() - .replaceFirst( - '', - '' - ) .replaceFirst( "navigator.serviceWorker.register('flutter_service_worker.js')", "navigator.serviceWorker.register('flutter_service_worker.js?v=$randomHash')", @@ -476,7 +472,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(); + self.skipWaiting(); return event.waitUntil( caches.open(TEMP).then((cache) => { return cache.addAll( @@ -545,6 +541,9 @@ self.addEventListener("activate", function(event) { // The fetch handler redirects requests for RESOURCE files to the service // worker cache. self.addEventListener("fetch", (event) => { + if (event.request.method !== 'GET') { + return; + } var origin = self.location.origin; var key = event.request.url.substring(origin.length + 1); // Redirect URLs to the index.html @@ -554,9 +553,10 @@ self.addEventListener("fetch", (event) => { if (event.request.url == origin || event.request.url.startsWith(origin + '/#') || key == '') { key = '/'; } - // If the URL is not the RESOURCE list, skip the cache. + // If the URL is not the RESOURCE list then return to signal that the + // browser should take over. if (!RESOURCES[key]) { - return event.respondWith(fetch(event.request)); + return; } // If the URL is the index.html, perform an online-first request. if (key == '/') { @@ -580,7 +580,7 @@ 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') { - skipWaiting(); + self.skipWaiting(); return; } if (event.data === 'downloadOffline') { @@ -631,11 +631,5 @@ function onlineFirst(event) { }) ); } - -function skipWaiting() { - if (self.skipWaiting != undefined) { - self.skipWaiting(); - } -} '''; } diff --git a/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart b/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart index 4a8684da64..9c686a7c35 100644 --- a/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart +++ b/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart @@ -136,9 +136,9 @@ void main() { expect(environment.outputDir.childFile('foo.txt') .readAsStringSync(), 'B'); - // Appends number to requests for service worker and main.dart.js + // Appends number to requests for service worker only expect(environment.outputDir.childFile('index.html').readAsStringSync(), allOf( - contains('