From 0d52630ef1ea04b28ad9fb77bd02d8b17b350c05 Mon Sep 17 00:00:00 2001 From: Pavel Mazhnik Date: Mon, 30 Oct 2023 23:53:16 +0300 Subject: [PATCH] [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 --- .../lib/src/build_system/targets/web.dart | 2 +- .../build_system/targets/web_test.dart | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 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 fe5410f69b..fe138dac1d 100644 --- a/packages/flutter_tools/lib/src/build_system/targets/web.dart +++ b/packages/flutter_tools/lib/src/build_system/targets/web.dart @@ -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; } } 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 2c5a297251..4d58f2b8d9 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 @@ -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); }));