initial commit
This commit is contained in:
commit
713a58768a
4 changed files with 3490 additions and 0 deletions
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
node_modules/
|
||||
dist/
|
||||
.astro/
|
||||
package-lock.json
|
||||
|
||||
*~
|
||||
*swo
|
||||
*swp
|
||||
|
64
index.js
Normal file
64
index.js
Normal file
|
@ -0,0 +1,64 @@
|
|||
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";
|
||||
|
||||
/**
|
||||
* @type {Array<string>}
|
||||
*/
|
||||
let assets = [];
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* assetCachePrefix?: string,
|
||||
* assetCacheVersionID?: string,
|
||||
* path: string,
|
||||
* }} ServiceWorkerConfig
|
||||
*/
|
||||
|
||||
const plugin_dir = path.resolve(path.dirname('.'));
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {ServiceWorkerConfig} config
|
||||
* @returns {AstroIntegration}
|
||||
*/
|
||||
export default function serviceWorker(config) {
|
||||
let {
|
||||
assetCachePrefix,
|
||||
assetCacheVersionID = randomUUID(),
|
||||
path: serviceWorkerPath
|
||||
} = config;
|
||||
|
||||
console.log('[astro-sw] dir', plugin_dir)
|
||||
|
||||
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`;
|
||||
const prefixDeclaration = `const __prefix = ${JSON.stringify(assetCachePrefix)};\n`;
|
||||
|
||||
await writeFile(
|
||||
outFile,
|
||||
assetsDeclaration + versionDeclaration + prefixDeclaration + originalScript
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
21
package.json
Normal file
21
package.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "@ayco/astro-sw",
|
||||
"version": "0.0.1",
|
||||
"description": "Simple Astro integration to use your own authored service-worker",
|
||||
"main": "index.ts",
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [
|
||||
"withastro",
|
||||
"perf"
|
||||
],
|
||||
"author": "Ayo Ayco",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"astro": "^4.13.1"
|
||||
}
|
||||
}
|
3396
pnpm-lock.yaml
Normal file
3396
pnpm-lock.yaml
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue