feat: refactor astro build integration & implement cache versioning
This commit is contained in:
parent
391cfe628b
commit
799ac18c89
3 changed files with 39 additions and 20 deletions
|
@ -1,10 +1,7 @@
|
|||
import { defineConfig } from "astro/config";
|
||||
import cozyBuild from './plugins/cozy-build.ts';
|
||||
|
||||
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({
|
||||
|
@ -13,19 +10,6 @@ export default defineConfig({
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
cozyBuild()
|
||||
]
|
||||
});
|
34
plugins/cozy-build.ts
Normal file
34
plugins/cozy-build.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { AstroIntegration } from 'astro';
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
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 originalScript = await readFile(outFile);
|
||||
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,12 +1,13 @@
|
|||
|
||||
const cacheName = 'cozy-reader-app-v' + (version ?? '000')
|
||||
const addResourcesToCache = async (resources) => {
|
||||
const cache = await caches.open('cozy-reader-app-v1');
|
||||
const cache = await caches.open(cacheName);
|
||||
console.log('adding resources to cache...', resources)
|
||||
await cache.addAll(resources);
|
||||
};
|
||||
|
||||
const putInCache = async (request, response) => {
|
||||
const cache = await caches.open('cozy-reader-app-v1');
|
||||
const cache = await caches.open(cacheName);
|
||||
console.log('adding one response to cache...', request)
|
||||
await cache.put(request, response);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue