feat: dynamically fill up initial cache with generated assets
This commit is contained in:
parent
e25fa1a941
commit
fc9684fa99
3 changed files with 43 additions and 23 deletions
|
@ -1,11 +1,31 @@
|
||||||
import { defineConfig } from "astro/config";
|
import { defineConfig } from "astro/config";
|
||||||
|
|
||||||
import node from "@astrojs/node";
|
import node from "@astrojs/node";
|
||||||
|
import { readFile, writeFile } from 'node:fs/promises';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
|
||||||
|
let assets = []
|
||||||
|
|
||||||
// https://astro.build/config
|
// https://astro.build/config
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
output: "server",
|
output: "server",
|
||||||
adapter: node({
|
adapter: node({
|
||||||
mode: "middleware"
|
mode: "middleware"
|
||||||
})
|
}),
|
||||||
|
integrations: [
|
||||||
|
{
|
||||||
|
'name': 'astro-cozy-build',
|
||||||
|
'hooks': {
|
||||||
|
'astro:build:ssr': (options) => {
|
||||||
|
assets = options.manifest.assets.filter(ass => !ass.includes('sw.js'))
|
||||||
|
console.log('build-cozy', assets)
|
||||||
|
},
|
||||||
|
'astro:build:done': async ({dir}) => {
|
||||||
|
const outFile = fileURLToPath(new URL('./sw.js', dir));
|
||||||
|
const originalScript = await readFile(outFile);
|
||||||
|
await writeFile(outFile, 'const assets = ' + JSON.stringify(assets) + ';' + originalScript);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
});
|
});
|
|
@ -70,7 +70,7 @@ self.addEventListener('install', (event) => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
addResourcesToCache([
|
addResourcesToCache([
|
||||||
'./',
|
'./',
|
||||||
'./favicon.ico',
|
...(assets ?? [])
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
@ -48,26 +48,26 @@ navigator.serviceWorker.getRegistrations().then(registrations => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// const registerSW = async () => {
|
const registerSW = async () => {
|
||||||
// if ("serviceWorker" in navigator) {
|
if ("serviceWorker" in navigator) {
|
||||||
// try {
|
try {
|
||||||
// const registration = await navigator.serviceWorker.register("/sw.js", {
|
const registration = await navigator.serviceWorker.register("/sw.js", {
|
||||||
// scope: "/",
|
scope: "/",
|
||||||
// });
|
});
|
||||||
// if (registration.installing) {
|
if (registration.installing) {
|
||||||
// console.log("Service worker installing");
|
console.log("Service worker installing");
|
||||||
// } else if (registration.waiting) {
|
} else if (registration.waiting) {
|
||||||
// console.log("Service worker installed");
|
console.log("Service worker installed");
|
||||||
// } else if (registration.active) {
|
} else if (registration.active) {
|
||||||
// console.log("Service worker active");
|
console.log("Service worker active");
|
||||||
// }
|
}
|
||||||
// } catch (error) {
|
} catch (error) {
|
||||||
// console.error(`Registration failed with ${error}`);
|
console.error(`Registration failed with ${error}`);
|
||||||
// }
|
}
|
||||||
// } else {
|
} else {
|
||||||
// console.log('browser doesn\'t support service workers. boo')
|
console.log('browser doesn\'t support service workers. boo')
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// registerSW();
|
registerSW();
|
||||||
</script>
|
</script>
|
Loading…
Reference in a new issue