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 node from "@astrojs/node";
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
let assets = []
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
output: "server",
|
||||
adapter: node({
|
||||
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(
|
||||
addResourcesToCache([
|
||||
'./',
|
||||
'./favicon.ico',
|
||||
...(assets ?? [])
|
||||
])
|
||||
);
|
||||
});
|
||||
|
|
|
@ -48,26 +48,26 @@ navigator.serviceWorker.getRegistrations().then(registrations => {
|
|||
}
|
||||
});
|
||||
|
||||
// const registerSW = async () => {
|
||||
// if ("serviceWorker" in navigator) {
|
||||
// try {
|
||||
// const registration = await navigator.serviceWorker.register("/sw.js", {
|
||||
// scope: "/",
|
||||
// });
|
||||
// if (registration.installing) {
|
||||
// console.log("Service worker installing");
|
||||
// } else if (registration.waiting) {
|
||||
// console.log("Service worker installed");
|
||||
// } else if (registration.active) {
|
||||
// console.log("Service worker active");
|
||||
// }
|
||||
// } catch (error) {
|
||||
// console.error(`Registration failed with ${error}`);
|
||||
// }
|
||||
// } else {
|
||||
// console.log('browser doesn\'t support service workers. boo')
|
||||
// }
|
||||
// }
|
||||
const registerSW = async () => {
|
||||
if ("serviceWorker" in navigator) {
|
||||
try {
|
||||
const registration = await navigator.serviceWorker.register("/sw.js", {
|
||||
scope: "/",
|
||||
});
|
||||
if (registration.installing) {
|
||||
console.log("Service worker installing");
|
||||
} else if (registration.waiting) {
|
||||
console.log("Service worker installed");
|
||||
} else if (registration.active) {
|
||||
console.log("Service worker active");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Registration failed with ${error}`);
|
||||
}
|
||||
} else {
|
||||
console.log('browser doesn\'t support service workers. boo')
|
||||
}
|
||||
}
|
||||
|
||||
// registerSW();
|
||||
registerSW();
|
||||
</script>
|
Loading…
Reference in a new issue