refactor: cozy-build -> astro-sw astro integration
This commit is contained in:
parent
dca154b6fa
commit
612b7e64b4
4 changed files with 50 additions and 39 deletions
|
@ -1,6 +1,7 @@
|
||||||
import { defineConfig } from "astro/config";
|
import { defineConfig } from "astro/config";
|
||||||
import node from "@astrojs/node";
|
import node from "@astrojs/node";
|
||||||
import cozyBuild from "./src/plugins/cozy-build.ts";
|
import { randomUUID } from "node:crypto";
|
||||||
|
import serviceWorker from "./src/plugins/astro-sw.ts";
|
||||||
|
|
||||||
// https://astro.build/config
|
// https://astro.build/config
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
@ -9,6 +10,9 @@ export default defineConfig({
|
||||||
mode: "middleware"
|
mode: "middleware"
|
||||||
}),
|
}),
|
||||||
integrations: [
|
integrations: [
|
||||||
cozyBuild()
|
serviceWorker({
|
||||||
|
assetCacheVersionID: randomUUID(),
|
||||||
|
serviceWorkerPath: './src/utils/sw.js'
|
||||||
|
})
|
||||||
]
|
]
|
||||||
});
|
});
|
39
src/plugins/astro-sw.ts
Normal file
39
src/plugins/astro-sw.ts
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import { AstroIntegration } from 'astro';
|
||||||
|
import { readFile, writeFile } from 'node:fs/promises';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
import path from 'node:path';
|
||||||
|
|
||||||
|
let assets: string[] = [];
|
||||||
|
|
||||||
|
export type ServiceWorkerConfig = {
|
||||||
|
assetCacheVersionID: string,
|
||||||
|
serviceWorkerPath: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
export default (config?: ServiceWorkerConfig): AstroIntegration => {
|
||||||
|
const { assetCacheVersionID, serviceWorkerPath } = config ?? {};
|
||||||
|
|
||||||
|
return {
|
||||||
|
'name': 'astro-sw',
|
||||||
|
'hooks': {
|
||||||
|
'astro:build:ssr': ({ manifest }) => {
|
||||||
|
assets = manifest.assets.filter(ass => !ass.includes('sw.js'))
|
||||||
|
},
|
||||||
|
'astro:build:done': async ({ dir }) => {
|
||||||
|
const outFile = fileURLToPath(new URL('./sw.js', dir));
|
||||||
|
let originalScript;
|
||||||
|
try {
|
||||||
|
const __dirname = path.resolve(path.dirname('.'));
|
||||||
|
const swPath = path.join(__dirname,serviceWorkerPath ?? '');
|
||||||
|
console.log('[astro-sw] Using service worker:', swPath);
|
||||||
|
originalScript = await readFile(swPath);
|
||||||
|
} catch {
|
||||||
|
throw Error('[astro-sw] ERROR: service worker script not found!')
|
||||||
|
}
|
||||||
|
const assetsDeclaration = `const assets = ${JSON.stringify(assets)};\n`;
|
||||||
|
const versionDeclaration = `const version = ${JSON.stringify(assetCacheVersionID)};\n`;
|
||||||
|
await writeFile(outFile, assetsDeclaration + versionDeclaration + originalScript);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,37 +0,0 @@
|
||||||
import { AstroIntegration } from 'astro';
|
|
||||||
import { readFile, writeFile } from 'node:fs/promises';
|
|
||||||
import { fileURLToPath } from 'node:url';
|
|
||||||
import path from 'node:path';
|
|
||||||
|
|
||||||
let assets: string[] = [];
|
|
||||||
|
|
||||||
function getBuildTime() {
|
|
||||||
const today = new Date();
|
|
||||||
const dd = String(today.getDate()).padStart(2, '0');
|
|
||||||
const mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
|
|
||||||
const yyyy = today.getFullYear();
|
|
||||||
const time = today.getTime();
|
|
||||||
|
|
||||||
return mm + dd + yyyy + time;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
const config: AstroIntegration = {
|
|
||||||
'name': 'astro-cozy-build',
|
|
||||||
'hooks': {
|
|
||||||
'astro:build:ssr': ({ manifest }) => {
|
|
||||||
assets = 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 __dirname = path.resolve(path.dirname('.'));
|
|
||||||
const originalScript = await readFile(__dirname + '/src/plugins/sw.js');
|
|
||||||
const assetsDeclaration = `const assets = ${JSON.stringify(assets)};\n`;
|
|
||||||
const versionDeclaration = `const version = ${JSON.stringify(getBuildTime())};\n`;
|
|
||||||
await writeFile(outFile, assetsDeclaration + versionDeclaration + originalScript);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default (): AstroIntegration => config;
|
|
|
@ -1,4 +1,9 @@
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: create virtual imports for `version` & `assets`
|
||||||
|
* - currently injected by ../plugins/astro-sw.ts
|
||||||
|
*/
|
||||||
|
|
||||||
const cacheName = 'cozy-reader-app-v' + (version ?? '000')
|
const cacheName = 'cozy-reader-app-v' + (version ?? '000')
|
||||||
const addResourcesToCache = async (resources) => {
|
const addResourcesToCache = async (resources) => {
|
||||||
const cache = await caches.open(cacheName);
|
const cache = await caches.open(cacheName);
|
Loading…
Reference in a new issue