Skip to content

Commit 0d52630

Browse files
authored
[web] cache the base URL as root index.html (#136594)
Fixes flutter/flutter#136593 Caching of the base url was introduced in flutter/flutter#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
1 parent f830e4b commit 0d52630

File tree

2 files changed

+12
-4
lines changed
  • packages/flutter_tools

2 files changed

+12
-4
lines changed

packages/flutter_tools/lib/src/build_system/targets/web.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ class WebServiceWorker extends Target {
586586
final String hash = md5.convert(await file.readAsBytes()).toString();
587587
urlToHash[url] = hash;
588588
// Add an additional entry for the base URL.
589-
if (environment.fileSystem.path.basename(url) == 'index.html') {
589+
if (url == 'index.html') {
590590
urlToHash['/'] = hash;
591591
}
592592
}

packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,14 +1020,22 @@ void main() {
10201020
environment.outputDir
10211021
.childFile('index.html')
10221022
.createSync(recursive: true);
1023+
environment.outputDir
1024+
.childFile('assets/index.html')
1025+
..createSync(recursive: true)
1026+
..writeAsStringSync('A');
10231027
await WebServiceWorker(globals.fs, WebRendererMode.auto, isWasm: false).build(environment);
10241028

10251029
expect(environment.outputDir.childFile('flutter_service_worker.js'), exists);
1026-
// Contains file hash for both `/` and index.html.
1030+
// Contains the same file hash for both `/` and the root index.html file.
1031+
const String rootIndexHash = 'd41d8cd98f00b204e9800998ecf8427e';
1032+
expect(environment.outputDir.childFile('flutter_service_worker.js').readAsStringSync(),
1033+
contains('"/": "$rootIndexHash"'));
10271034
expect(environment.outputDir.childFile('flutter_service_worker.js').readAsStringSync(),
1028-
contains('"/": "d41d8cd98f00b204e9800998ecf8427e"'));
1035+
contains('"index.html": "$rootIndexHash"'));
1036+
// Make sure `assets/index.html` has a different hash than `index.html`.
10291037
expect(environment.outputDir.childFile('flutter_service_worker.js').readAsStringSync(),
1030-
contains('"index.html": "d41d8cd98f00b204e9800998ecf8427e"'));
1038+
contains('"assets/index.html": "7fc56270e7a70fa81a5935b72eacbe29"'));
10311039
expect(environment.buildDir.childFile('service_worker.d'), exists);
10321040
}));
10331041

0 commit comments

Comments
 (0)