1
0
forked from baron/baron-sso

fix(userfront): reduce service worker install cache

This commit is contained in:
2026-05-28 16:53:37 +09:00
parent 7401454bc0
commit 615d204678
3 changed files with 81 additions and 3 deletions

View File

@@ -105,7 +105,8 @@ if (existsSync(indexPath)) {
let index = readFileSync(indexPath, 'utf8');
const preloadLinks = [
'<link rel="preload" href="flutter_bootstrap.js" as="script" />',
hashedEntrypoints.has('main.dart.js')
hashedEntrypoints.has('main.dart.js') &&
!(hashedEntrypoints.has('main.dart.mjs') && hashedEntrypoints.has('main.dart.wasm'))
? `<link rel="preload" href="${hashedEntrypoints.get('main.dart.js')}" as="script" />`
: '',
hashedEntrypoints.has('main.dart.mjs')
@@ -209,6 +210,10 @@ function createServiceWorker() {
}
const assetPath = `/${relative(buildDir, filePath).replaceAll('\\', '/')}`;
if (!shouldPrecacheAsset(assetPath)) {
continue;
}
assets.push(assetPath);
versionHash.update(assetPath);
versionHash.update(readFileSync(filePath));
@@ -305,6 +310,27 @@ async function cacheFirst(request) {
`;
}
function shouldPrecacheAsset(assetPath) {
if (
[
'/index.html',
'/flutter_bootstrap.js',
'/manifest.json',
'/version.json',
'/assets/AssetManifest.bin.json',
'/assets/FontManifest.json',
].includes(assetPath)
) {
return true;
}
if (/^\/assets\/assets\/translations\/(?:en|ko|template)\.toml$/.test(assetPath)) {
return true;
}
return false;
}
function ensureServiceWorkerUrl(settings) {
const serviceWorkerUrl = `"/flutter_service_worker.js?v=" + ${serviceWorkerVersionExpression(settings)}`;
if (/serviceWorkerUrl\s*:/.test(settings)) {