feat: support astro 6; simplify APIs for exclude & include URLs
Some checks are pending
Demo / Explore-CI (push) Waiting to run

This commit is contained in:
ayo 2026-04-05 22:58:06 +02:00
parent c8424c53a0
commit e8b504426c
4 changed files with 1052 additions and 1084 deletions

View file

@ -10,8 +10,8 @@
"description": "Use your own authored service worker with Astro",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
"types": "./dist/astro-sw.d.ts",
"default": "./dist/astro-sw.js"
},
"./globals": {
"types": "./dist/eslint/globals.d.ts",
@ -24,11 +24,16 @@
"./presets/*": {
"types": "./dist/presets/*/index.d.ts",
"default": "./dist/presets/*/index.js"
},
"./package.json": {
"default": "./package.json"
}
},
"files": [
"dist",
"README.md"
"LICENSE",
"README.md",
"package.json"
],
"main": "./astro-sw.js",
"type": "module",
@ -36,7 +41,7 @@
"node": ">=18.0.0"
},
"scripts": {
"build": "tsup src/index.ts src/presets/index.ts src/presets/**/index.ts src/eslint/globals.ts --format esm --dts --clean && cp ../README.md .",
"build": "tsup src/astro-sw.ts src/presets/index.ts src/presets/**/index.ts src/eslint/globals.ts --format esm --dts --clean && cp ../README.md .",
"test": "vitest run",
"publish": "npm publish",
"version:patch": "npm version patch",
@ -51,7 +56,7 @@
"esbuild": "^0.27.4"
},
"peerDependencies": {
"astro": "^5.6"
"astro": "^6"
},
"devDependencies": {
"@types/node": "^25.5.0"

222
package/src/astro-sw.ts Normal file
View file

@ -0,0 +1,222 @@
/**
* @license MIT <https://opensource.org/licenses/MIT>
* @author Ayo Ayco <https://ayo.ayco.io>
*/
import { readFile, writeFile, unlink } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { resolve, dirname, join } from 'node:path'
import { build } from 'esbuild'
import type { AstroServiceWorkerConfig } from './types'
import type { AstroIntegration } from 'astro'
const ASTROSW = '@ayco/astro-sw'
/**
* TODO: update JSDoc
* Accepts configuration options with service worker path
* and injects needed variables such as `__assets` generated by Astro
*/
export default function serviceWorker(
// TODO handle options undefined
options?: AstroServiceWorkerConfig
): AstroIntegration {
const {
presets,
assetCachePrefix = ASTROSW,
assetCacheVersionID = '0',
path: serviceWorkerPath = undefined,
exclude = [],
include = [],
logAssets = false,
esbuild = {},
registrationHooks = {},
} = options ?? {}
const {
installing: installingFn = () => { },
waiting: waitingFn = () => { },
active: activeFn = () => { },
error: errorFn = () => { },
unsupported: unsupportedFn = () => { },
afterRegistration: afterRegistrationFn = () => { },
} = registrationHooks
// TODO use presets
console.log(presets)
/**
* @type {Array<string>}
*/
let ssrAssets: string[] = []
const registrationScript = `const registerSW = async () => {
if ("serviceWorker" in navigator) {
try {
const registration = await navigator.serviceWorker.register("/sw.js", {
scope: "/",
});
if (registration.installing) {
(${installingFn.toString()})();
} else if (registration.waiting) {
(${waitingFn.toString()})();
} else if (registration.active) {
(${activeFn.toString()})();
}
(${afterRegistrationFn.toString()})();
} catch (error) {
(${errorFn.toString()})(error);
}
} else {
(${unsupportedFn.toString()})();
}
}
registerSW();`
// let output = 'static'
const __dirname = resolve(dirname('.'))
return {
name: ASTROSW,
hooks: {
'astro:config:setup': async ({ injectScript, command, logger }) => {
if (!serviceWorkerPath || serviceWorkerPath === '') {
// REQUIRED OPTION IS MISSING
logger.error('Missing required path to service worker script')
}
// const transformedScript=await transform(registrationScript)
// output = _config.output
if (command === 'build') {
injectScript('page', registrationScript)
}
},
'astro:config:done': async ({ injectTypes }) => {
const injectedTypes = `
declare const __assets: string[];
declare const __version: string;
declare const __prefix: string;`
injectTypes({ filename: 'caching.d.ts', content: injectedTypes })
},
'astro:build:ssr': ({ manifest }) => {
ssrAssets = manifest.assets
},
'astro:build:done': async ({
dir,
pages,
logger,
}) => {
const outfile = fileURLToPath(new URL('./sw.js', dir))
const swPath =
serviceWorkerPath && serviceWorkerPath !== ''
? join(__dirname, serviceWorkerPath)
: undefined
let originalScript
const _pages =
pages
.filter(({ pathname }) => pathname !== '')
.map(({ pathname }) => `/${pathname}`) ?? []
/**
* By default the `pages` have a slash in the end.
* We also want to cache routes that don't have a slash.
*/
const _pagesWithoutEndSlash =
pages
.filter(({ pathname }) => pathname !== '')
.map(({ pathname }) => {
const lastChar = pathname.slice(-1)
const len = pathname.length
return lastChar === '/'
? `/${pathname.slice(0, len - 1)}`
: `/${pathname}`
}) ?? []
const _excludeRoutes = [
...exclude,
...exclude.map((route) => `${route}/`),
]
const __assets = [
...new Set([
...ssrAssets,
...include,
..._pages,
..._pagesWithoutEndSlash,
]),
].filter(
(asset) =>
!!asset &&
asset !== '' &&
!asset.includes('404') &&
!asset.includes('index.html') &&
!_excludeRoutes.includes(asset)
)
if (logAssets) {
logger.info(
`${__assets.length} assets for caching: \n ▶ ${__assets.toString().replaceAll(',', '\n ▶ ')}\n`
)
} else {
logger.info(`${__assets.length} assets for caching.`)
}
try {
logger.info(`Using service worker in path: ${swPath}`)
// @ts-expect-error undefined error is caught via try-catch
originalScript = await readFile(swPath)
} catch (err: unknown) {
logger.error(JSON.stringify(err))
if (!swPath) {
logger.error(`
[${ASTROSW}] ERR: The 'path' option is required!
[${ASTROSW}] INFO: Please see service worker options in https://ayco.io/gh/astro-sw#readme
`)
}
}
const assetsDeclaration = `const __assets = ${JSON.stringify(__assets)};\n`
const versionDeclaration = `const __version = ${JSON.stringify(assetCacheVersionID)};\n`
const prefixDeclaration = `const __prefix = ${JSON.stringify(assetCachePrefix)};\n`
const tempFile = `${swPath}.tmp.ts`
try {
await writeFile(
tempFile,
assetsDeclaration +
versionDeclaration +
prefixDeclaration +
originalScript,
{ flag: 'w+' }
)
} catch (err) {
logger.error(JSON.stringify(err))
}
try {
await build({
bundle: true,
...esbuild,
outfile,
platform: 'browser',
entryPoints: [tempFile],
})
} catch (err) {
logger.error(JSON.stringify(err))
}
// remove temp file
try {
await unlink(tempFile)
} catch (err) {
logger.error(JSON.stringify(err))
}
},
},
}
}

View file

@ -16,7 +16,14 @@ export type AstroServiceWorkerConfig = {
assetCachePrefix?: string
assetCacheVersionID?: string
customRoutes?: string[]
excludeRoutes?: string[]
/**
* URL of resources to exclude in the cache
*/
exclude?: string[]
/**
* URL of resources not generated by Astro to add in the cache
*/
include?: string[],
logAssets?: true
esbuild?: BuildOptions
registrationHooks?: {

File diff suppressed because it is too large Load diff