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 { defineConfig } from "astro/config";
|
||||||
import node from "@astrojs/node";
|
import node from "@astrojs/node";
|
||||||
import { randomUUID } from "node:crypto";
|
|
||||||
import serviceWorker from "./src/plugins/astro-sw.ts";
|
import serviceWorker from "./src/plugins/astro-sw.ts";
|
||||||
|
|
||||||
// https://astro.build/config
|
// https://astro.build/config
|
||||||
|
@ -11,7 +10,7 @@ export default defineConfig({
|
||||||
}),
|
}),
|
||||||
integrations: [
|
integrations: [
|
||||||
serviceWorker({
|
serviceWorker({
|
||||||
assetCacheVersionID: randomUUID(),
|
assetCachePrefix: 'cozy-reader',
|
||||||
serviceWorkerPath: './src/utils/sw.js'
|
serviceWorkerPath: './src/utils/sw.js'
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
|
|
|
@ -2,16 +2,26 @@ import { AstroIntegration } from 'astro';
|
||||||
import { readFile, writeFile } from 'node:fs/promises';
|
import { readFile, writeFile } from 'node:fs/promises';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
import { randomUUID } from "node:crypto";
|
||||||
|
|
||||||
let assets: string[] = [];
|
let assets: string[] = [];
|
||||||
|
|
||||||
export type ServiceWorkerConfig = {
|
export type ServiceWorkerConfig = {
|
||||||
assetCacheVersionID: string,
|
assetCachePrefix?: string,
|
||||||
|
assetCacheVersionID?: string,
|
||||||
serviceWorkerPath: string,
|
serviceWorkerPath: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default (config?: ServiceWorkerConfig): AstroIntegration => {
|
const plugin_dir = path.resolve(path.dirname('.'));
|
||||||
const { assetCacheVersionID, serviceWorkerPath } = config ?? {};
|
|
||||||
|
export default (config: ServiceWorkerConfig): AstroIntegration => {
|
||||||
|
let {
|
||||||
|
assetCachePrefix,
|
||||||
|
assetCacheVersionID = randomUUID(),
|
||||||
|
serviceWorkerPath
|
||||||
|
} = config;
|
||||||
|
|
||||||
|
console.log('[astro-sw] dir', plugin_dir)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'name': 'astro-sw',
|
'name': 'astro-sw',
|
||||||
|
@ -32,7 +42,12 @@ export default (config?: ServiceWorkerConfig): AstroIntegration => {
|
||||||
}
|
}
|
||||||
const assetsDeclaration = `const assets = ${JSON.stringify(assets)};\n`;
|
const assetsDeclaration = `const assets = ${JSON.stringify(assets)};\n`;
|
||||||
const versionDeclaration = `const version = ${JSON.stringify(assetCacheVersionID)};\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
|
* - 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 addResourcesToCache = async (resources) => {
|
||||||
const cache = await caches.open(cacheName);
|
const cache = await caches.open(cacheName);
|
||||||
console.log('adding resources to cache...', resources)
|
console.log('adding resources to cache...', resources)
|
||||||
|
|
Loading…
Reference in a new issue