feat: implement assetCachePrefix & set sw config defaults
This commit is contained in:
parent
6562f35c0c
commit
ea673e6bdd
3 changed files with 22 additions and 8 deletions
|
@ -1,6 +1,5 @@
|
|||
import { defineConfig } from "astro/config";
|
||||
import node from "@astrojs/node";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import serviceWorker from "./src/plugins/astro-sw.ts";
|
||||
|
||||
// https://astro.build/config
|
||||
|
@ -11,7 +10,7 @@ export default defineConfig({
|
|||
}),
|
||||
integrations: [
|
||||
serviceWorker({
|
||||
assetCacheVersionID: randomUUID(),
|
||||
assetCachePrefix: 'cozy-reader',
|
||||
serviceWorkerPath: './src/utils/sw.js'
|
||||
})
|
||||
]
|
||||
|
|
|
@ -2,16 +2,26 @@ import { AstroIntegration } from 'astro';
|
|||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import path from 'node:path';
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
let assets: string[] = [];
|
||||
|
||||
export type ServiceWorkerConfig = {
|
||||
assetCacheVersionID: string,
|
||||
assetCachePrefix?: string,
|
||||
assetCacheVersionID?: string,
|
||||
serviceWorkerPath: string,
|
||||
}
|
||||
|
||||
export default (config?: ServiceWorkerConfig): AstroIntegration => {
|
||||
const { assetCacheVersionID, serviceWorkerPath } = config ?? {};
|
||||
const plugin_dir = path.resolve(path.dirname('.'));
|
||||
|
||||
export default (config: ServiceWorkerConfig): AstroIntegration => {
|
||||
let {
|
||||
assetCachePrefix,
|
||||
assetCacheVersionID = randomUUID(),
|
||||
serviceWorkerPath
|
||||
} = config;
|
||||
|
||||
console.log('[astro-sw] dir', plugin_dir)
|
||||
|
||||
return {
|
||||
'name': 'astro-sw',
|
||||
|
@ -32,7 +42,12 @@ export default (config?: ServiceWorkerConfig): AstroIntegration => {
|
|||
}
|
||||
const assetsDeclaration = `const assets = ${JSON.stringify(assets)};\n`;
|
||||
const versionDeclaration = `const version = ${JSON.stringify(assetCacheVersionID)};\n`;
|
||||
await writeFile(outFile, assetsDeclaration + versionDeclaration + originalScript);
|
||||
const prefixDeclaration = `const prefix = ${JSON.stringify(assetCachePrefix)};\n`;
|
||||
|
||||
await writeFile(
|
||||
outFile,
|
||||
assetsDeclaration + versionDeclaration + prefixDeclaration + originalScript
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
|
||||
/**
|
||||
* TODO: create virtual imports for `version` & `assets`
|
||||
* TODO: create virtual imports for `prefix`, `version` & `assets`
|
||||
* - currently injected by ../plugins/astro-sw.ts
|
||||
*/
|
||||
|
||||
const cacheName = 'cozy-reader-app-v' + (version ?? '000')
|
||||
const cacheName = `${prefix ?? 'app'}-v${version ?? '000'}`
|
||||
const addResourcesToCache = async (resources) => {
|
||||
const cache = await caches.open(cacheName);
|
||||
console.log('adding resources to cache...', resources)
|
||||
|
|
Loading…
Reference in a new issue