Skip to content

Commit

Permalink
patch flutter web to load faster
Browse files Browse the repository at this point in the history
  • Loading branch information
harryfei committed Mar 1, 2023
1 parent cd49c5b commit a2ecf6b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions build_tools/patch_web.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env sh

set -e

patch build/web/flutter.js < build_tools/web/flutter.js.patch
patch build/web/flutter_service_worker.js < build_tools/web/flutter_service_worker.js.patch
20 changes: 20 additions & 0 deletions build_tools/web/flutter.js.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--- build/web/flutter.js.orig 2023-03-01 15:40:58.588065610 +0800
+++ build/web/flutter.js 2023-03-01 15:43:48.879605009 +0800
@@ -375,7 +375,16 @@
// Install the `didCreateEngineInitializer` listener where Flutter web expects it to be.
this.didCreateEngineInitializer =
entrypointLoader.didCreateEngineInitializer.bind(entrypointLoader);
- return entrypointLoader.loadEntrypoint(entrypoint);
+ const main = entrypointLoader.loadEntrypoint(entrypoint);
+
+ fetch("canvaskit/canvaskit.js");
+ fetch("canvaskit/canvaskit.wasm");
+ fetch("assets/FontManifest.json");
+ fetch("assets/fonts/MaterialIcons-Regular.otf");
+ fetch("assets/packages/cupertino_icons/assets/CupertinoIcons.ttf");
+ fetch("/");
+
+ return main;
}
}

62 changes: 62 additions & 0 deletions build_tools/web/flutter_service_worker.js.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--- build/web/flutter_service_worker.js.orig 2023-02-14 05:46:14.641558233 +0800
+++ build/web/flutter_service_worker.js 2023-02-14 05:48:16.339943489 +0800
@@ -29,21 +29,6 @@
"/": "edf4d01f5266c63075af78507efdc906"};
// The application shell files that are downloaded before a service worker can
// start.
-const CORE = ["main.dart.js",
-"index.html",
-"assets/AssetManifest.json",
-"assets/FontManifest.json"];
-
-// During install, the TEMP cache is populated with the application shell files.
-self.addEventListener("install", (event) => {
- self.skipWaiting();
- return event.waitUntil(
- caches.open(TEMP).then((cache) => {
- return cache.addAll(
- CORE.map((value) => new Request(value, {'cache': 'reload'})));
- })
- );
-});
// During activate, the cache is populated with the temp files downloaded in
// install. If this service worker is upgrading from one with a saved
// MANIFEST, then use this to retain unchanged resource files.
@@ -104,6 +89,7 @@
}
}());
});
+var IN_PROCESSING_REQUESTS = {};
// The fetch handler redirects requests for RESOURCE files to the service
// worker cache.
self.addEventListener("fetch", (event) => {
@@ -133,12 +119,27 @@
return cache.match(event.request).then((response) => {
// Either respond with the cached resource, or perform a fetch and
// lazily populate the cache only if the resource was successfully fetched.
- return response || fetch(event.request).then((response) => {
+
+ if (response) {
+ return response;
+ }
+
+ if (IN_PROCESSING_REQUESTS[key]) {
+ return IN_PROCESSING_REQUESTS[key].clone();
+ }
+
+ return fetch(event.request).then((response) => {
if (response && Boolean(response.ok)) {
- cache.put(event.request, response.clone());
+
+ cache.put(event.request, response.clone())
+ .then(() => delete IN_PROCESSING_REQUESTS[key]);
}
+
+ IN_PROCESSING_REQUESTS[key] = response.clone();
+
return response;
});
+
})
})
);
6 changes: 6 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ <h2>End-to-end encryption</h2>
_loadScript("https://cdn.jsdelivr.net/npm/[email protected]/dist/jsQR.min.js");
});
});

// load serviceworker earlier
const serviceWorkerActivation = navigator.serviceWorker.register(
"flutter_service_worker.js?v=" + serviceWorkerVersion
);

</script>
</body>
</html>

0 comments on commit a2ecf6b

Please sign in to comment.