Unverified Commit 84aae22c authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[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
parent 70f21de9
...@@ -334,16 +334,12 @@ class WebReleaseBundle extends Target { ...@@ -334,16 +334,12 @@ class WebReleaseBundle extends Target {
outputFile.parent.createSync(recursive: true); outputFile.parent.createSync(recursive: true);
} }
outputResourcesFiles.add(outputFile); outputResourcesFiles.add(outputFile);
// insert a random hash into the requests for main.dart.js and service_worker.js. This is // insert a random hash into the requests for service_worker.js. This is not a content hash,
// not a content hash, because it would need to be the hash for the entire bundle and not // because it would need to be the hash for the entire bundle and not just the resource
// just the resource in question. // in question.
if (environment.fileSystem.path.basename(inputFile.path) == 'index.html') { if (environment.fileSystem.path.basename(inputFile.path) == 'index.html') {
final String randomHash = Random().nextInt(4294967296).toString(); final String randomHash = Random().nextInt(4294967296).toString();
final String resultString = inputFile.readAsStringSync() final String resultString = inputFile.readAsStringSync()
.replaceFirst(
'<script src="main.dart.js" type="application/javascript"></script>',
'<script src="main.dart.js?v=$randomHash" type="application/javascript"></script>'
)
.replaceFirst( .replaceFirst(
"navigator.serviceWorker.register('flutter_service_worker.js')", "navigator.serviceWorker.register('flutter_service_worker.js')",
"navigator.serviceWorker.register('flutter_service_worker.js?v=$randomHash')", "navigator.serviceWorker.register('flutter_service_worker.js?v=$randomHash')",
...@@ -476,7 +472,7 @@ const CORE = [ ...@@ -476,7 +472,7 @@ const CORE = [
${coreBundle.map((String file) => '"$file"').join(',\n')}]; ${coreBundle.map((String file) => '"$file"').join(',\n')}];
// During install, the TEMP cache is populated with the application shell files. // During install, the TEMP cache is populated with the application shell files.
self.addEventListener("install", (event) => { self.addEventListener("install", (event) => {
skipWaiting(); self.skipWaiting();
return event.waitUntil( return event.waitUntil(
caches.open(TEMP).then((cache) => { caches.open(TEMP).then((cache) => {
return cache.addAll( return cache.addAll(
...@@ -545,6 +541,9 @@ self.addEventListener("activate", function(event) { ...@@ -545,6 +541,9 @@ self.addEventListener("activate", function(event) {
// The fetch handler redirects requests for RESOURCE files to the service // The fetch handler redirects requests for RESOURCE files to the service
// worker cache. // worker cache.
self.addEventListener("fetch", (event) => { self.addEventListener("fetch", (event) => {
if (event.request.method !== 'GET') {
return;
}
var origin = self.location.origin; var origin = self.location.origin;
var key = event.request.url.substring(origin.length + 1); var key = event.request.url.substring(origin.length + 1);
// Redirect URLs to the index.html // Redirect URLs to the index.html
...@@ -554,9 +553,10 @@ self.addEventListener("fetch", (event) => { ...@@ -554,9 +553,10 @@ self.addEventListener("fetch", (event) => {
if (event.request.url == origin || event.request.url.startsWith(origin + '/#') || key == '') { if (event.request.url == origin || event.request.url.startsWith(origin + '/#') || key == '') {
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]) { if (!RESOURCES[key]) {
return event.respondWith(fetch(event.request)); return;
} }
// If the URL is the index.html, perform an online-first request. // If the URL is the index.html, perform an online-first request.
if (key == '/') { if (key == '/') {
...@@ -580,7 +580,7 @@ self.addEventListener('message', (event) => { ...@@ -580,7 +580,7 @@ self.addEventListener('message', (event) => {
// SkipWaiting can be used to immediately activate a waiting service worker. // SkipWaiting can be used to immediately activate a waiting service worker.
// This will also require a page refresh triggered by the main worker. // This will also require a page refresh triggered by the main worker.
if (event.data === 'skipWaiting') { if (event.data === 'skipWaiting') {
skipWaiting(); self.skipWaiting();
return; return;
} }
if (event.data === 'downloadOffline') { if (event.data === 'downloadOffline') {
...@@ -631,11 +631,5 @@ function onlineFirst(event) { ...@@ -631,11 +631,5 @@ function onlineFirst(event) {
}) })
); );
} }
function skipWaiting() {
if (self.skipWaiting != undefined) {
self.skipWaiting();
}
}
'''; ''';
} }
...@@ -136,9 +136,9 @@ void main() { ...@@ -136,9 +136,9 @@ void main() {
expect(environment.outputDir.childFile('foo.txt') expect(environment.outputDir.childFile('foo.txt')
.readAsStringSync(), 'B'); .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( expect(environment.outputDir.childFile('index.html').readAsStringSync(), allOf(
contains('<script src="main.dart.js?v='), contains('<script src="main.dart.js" type="application/javascript">'),
contains('flutter_service_worker.js?v='), contains('flutter_service_worker.js?v='),
)); ));
})); }));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment