[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
This commit is contained in:
@@ -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(
|
||||
'<script src="main.dart.js" type="application/javascript"></script>',
|
||||
'<script src="main.dart.js?v=$randomHash" type="application/javascript"></script>'
|
||||
)
|
||||
.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();
|
||||
}
|
||||
}
|
||||
''';
|
||||
}
|
||||
|
||||
@@ -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('<script src="main.dart.js?v='),
|
||||
contains('<script src="main.dart.js" type="application/javascript">'),
|
||||
contains('flutter_service_worker.js?v='),
|
||||
));
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user