[web] cache the base URL as root index.html (#136594)

Fixes https://github.com/flutter/flutter/issues/136593

Caching of the base url was introduced in https://github.com/flutter/flutter/pull/53666 but resources can contain multiple `index.html` files, and currently hash of the **latest** asset will be assigned to the base url, which is not necessarily the root index.html
This commit is contained in:
Pavel Mazhnik 2023-10-30 23:53:16 +03:00 committed by GitHub
parent f830e4be4d
commit 0d52630ef1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View file

@ -586,7 +586,7 @@ class WebServiceWorker extends Target {
final String hash = md5.convert(await file.readAsBytes()).toString();
urlToHash[url] = hash;
// Add an additional entry for the base URL.
if (environment.fileSystem.path.basename(url) == 'index.html') {
if (url == 'index.html') {
urlToHash['/'] = hash;
}
}

View file

@ -1020,14 +1020,22 @@ void main() {
environment.outputDir
.childFile('index.html')
.createSync(recursive: true);
environment.outputDir
.childFile('assets/index.html')
..createSync(recursive: true)
..writeAsStringSync('A');
await WebServiceWorker(globals.fs, WebRendererMode.auto, isWasm: false).build(environment);
expect(environment.outputDir.childFile('flutter_service_worker.js'), exists);
// Contains file hash for both `/` and index.html.
// Contains the same file hash for both `/` and the root index.html file.
const String rootIndexHash = 'd41d8cd98f00b204e9800998ecf8427e';
expect(environment.outputDir.childFile('flutter_service_worker.js').readAsStringSync(),
contains('"/": "d41d8cd98f00b204e9800998ecf8427e"'));
contains('"/": "$rootIndexHash"'));
expect(environment.outputDir.childFile('flutter_service_worker.js').readAsStringSync(),
contains('"index.html": "d41d8cd98f00b204e9800998ecf8427e"'));
contains('"index.html": "$rootIndexHash"'));
// Make sure `assets/index.html` has a different hash than `index.html`.
expect(environment.outputDir.childFile('flutter_service_worker.js').readAsStringSync(),
contains('"assets/index.html": "7fc56270e7a70fa81a5935b72eacbe29"'));
expect(environment.buildDir.childFile('service_worker.d'), exists);
}));