All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m4s
29 lines
1.6 KiB
JavaScript
29 lines
1.6 KiB
JavaScript
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
import { createHash } from "node:crypto";
|
|
const assets = readdirSync("dist/assets").map((name) => "/assets/" + name);
|
|
const shell = [
|
|
"/",
|
|
"/index.html",
|
|
"/icon.svg",
|
|
"/manifest.webmanifest",
|
|
"/icons/apple-touch-icon.png",
|
|
"/icons/icon-192.png",
|
|
"/icons/icon-512.png",
|
|
"/icons/maskable-512.png",
|
|
...assets,
|
|
];
|
|
const digest = createHash("sha256");
|
|
for (const path of shell)
|
|
digest.update(readFileSync(path === "/" ? "dist/index.html" : "dist" + path));
|
|
const hash = digest.digest("hex").slice(0, 12);
|
|
writeFileSync(
|
|
"dist/sw.js",
|
|
`const CACHE='calio-${hash}';const SHELL=${JSON.stringify(shell)};
|
|
self.addEventListener('install',event=>event.waitUntil(caches.open(CACHE).then(cache=>cache.addAll(SHELL))));
|
|
self.addEventListener('message',event=>{if(event.data==='ACTIVATE')self.skipWaiting()});
|
|
self.addEventListener('activate',event=>event.waitUntil(caches.keys().then(keys=>Promise.all(keys.filter(key=>key.startsWith('calio-')&&key!==CACHE).map(key=>caches.delete(key)))).then(()=>self.clients.claim())));
|
|
self.addEventListener('fetch',event=>{const url=new URL(event.request.url);if(event.request.method!=='GET'||url.origin!==self.location.origin||url.pathname.startsWith('/api/'))return;
|
|
if(event.request.mode==='navigate'){event.respondWith(caches.open(CACHE).then(cache=>cache.match('/index.html')).then(cached=>cached||fetch(event.request)));return;}
|
|
if(SHELL.includes(url.pathname))event.respondWith(caches.open(CACHE).then(cache=>cache.match(event.request)).then(cached=>cached||fetch(event.request)));
|
|
});`,
|
|
);
|