[flutter_tools] don't force service worker cache files to be absolute (#52606)
diff --git a/dev/integration_tests/flutter_gallery/web/index.html b/dev/integration_tests/flutter_gallery/web/index.html
index 328096a..23bcf28 100644
--- a/dev/integration_tests/flutter_gallery/web/index.html
+++ b/dev/integration_tests/flutter_gallery/web/index.html
@@ -12,11 +12,11 @@
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="Flutter Gallery">
- <link rel="apple-touch-icon" href="/icons/Icon-192.png">
- <link rel="shortcut icon" type="image/png" href="/favicon.png"/>
+ <link rel="apple-touch-icon" href="icons/Icon-192.png">
+ <link rel="shortcut icon" type="image/png" href="favicon.png"/>
<title>Flutter Gallery</title>
- <link rel="manifest" href="/manifest.json">
+ <link rel="manifest" href="manifest.json">
</head>
<body>
<!-- This script installs service_worker.js to provide PWA functionality to
@@ -25,7 +25,7 @@
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
- navigator.serviceWorker.register('/flutter_service_worker.js');
+ navigator.serviceWorker.register('flutter_service_worker.js');
});
}
</script>
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 01c1c31..1efed88 100644
--- a/packages/flutter_tools/lib/src/build_system/targets/web.dart
+++ b/packages/flutter_tools/lib/src/build_system/targets/web.dart
@@ -340,18 +340,26 @@
.where((File file) => !file.path.endsWith('flutter_service_worker.js')
&& !globals.fs.path.basename(file.path).startsWith('.'))
.toList();
- // TODO(jonahwilliams): determine whether this needs to be made more efficient.
- final Map<String, String> uriToHash = <String, String>{
- for (File file in contents)
- // Do not force caching of source maps.
- if (!file.path.endsWith('main.dart.js.map'))
- '/${globals.fs.path.toUri(globals.fs.path.relative(file.path, from: environment.outputDir.path)).toString()}':
- md5.convert(await file.readAsBytes()).toString(),
- };
+
+ final Map<String, String> urlToHash = <String, String>{};
+ for (final File file in contents) {
+ // Do not force caching of source maps.
+ if (file.path.endsWith('main.dart.js.map')) {
+ continue;
+ }
+ final String url = globals.fs.path.toUri(
+ globals.fs.path.relative(
+ file.path,
+ from: environment.outputDir.path),
+ ).toString();
+ final String hash = md5.convert(await file.readAsBytes()).toString();
+ urlToHash[url] = hash;
+ }
+
final File serviceWorkerFile = environment.outputDir
.childFile('flutter_service_worker.js');
final Depfile depfile = Depfile(contents, <File>[serviceWorkerFile]);
- final String serviceWorker = generateServiceWorker(uriToHash);
+ final String serviceWorker = generateServiceWorker(urlToHash);
serviceWorkerFile
.writeAsStringSync(serviceWorker);
final DepfileService depfileService = DepfileService(
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 f20b296..c56c39e 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
@@ -452,7 +452,7 @@
expect(environment.outputDir.childFile('flutter_service_worker.js'), exists);
// Contains file hash.
expect(environment.outputDir.childFile('flutter_service_worker.js').readAsStringSync(),
- contains('"/a/a.txt": "7fc56270e7a70fa81a5935b72eacbe29"'));
+ contains('"a/a.txt": "7fc56270e7a70fa81a5935b72eacbe29"'));
expect(environment.buildDir.childFile('service_worker.d'), exists);
// Depends on resource file.
expect(environment.buildDir.childFile('service_worker.d').readAsStringSync(), contains('a/a.txt'));