feat: use AstroIntegration.assets instead of deprecated routes
This commit is contained in:
parent
e3c72fe50a
commit
6d2a090387
2 changed files with 27 additions and 24 deletions
|
@ -68,7 +68,7 @@ export default function serviceWorker(options) {
|
||||||
/**
|
/**
|
||||||
* @type {Array<string>}
|
* @type {Array<string>}
|
||||||
*/
|
*/
|
||||||
let assets = []
|
let manifestAssets = []
|
||||||
|
|
||||||
const registrationScript = `const registerSW = async () => {
|
const registrationScript = `const registerSW = async () => {
|
||||||
if ("serviceWorker" in navigator) {
|
if ("serviceWorker" in navigator) {
|
||||||
|
@ -121,9 +121,14 @@ declare const __prefix: string;`
|
||||||
injectTypes({ filename: 'caching.d.ts', content: injectedTypes })
|
injectTypes({ filename: 'caching.d.ts', content: injectedTypes })
|
||||||
},
|
},
|
||||||
'astro:build:ssr': ({ manifest }) => {
|
'astro:build:ssr': ({ manifest }) => {
|
||||||
assets = manifest.assets
|
manifestAssets = manifest.assets
|
||||||
},
|
},
|
||||||
'astro:build:done': async ({ dir, routes, pages, logger }) => {
|
'astro:build:done': async ({
|
||||||
|
dir,
|
||||||
|
assets: astroAssets,
|
||||||
|
pages,
|
||||||
|
logger,
|
||||||
|
}) => {
|
||||||
const outfile = fileURLToPath(new URL('./sw.js', dir))
|
const outfile = fileURLToPath(new URL('./sw.js', dir))
|
||||||
const swPath =
|
const swPath =
|
||||||
serviceWorkerPath && serviceWorkerPath !== ''
|
serviceWorkerPath && serviceWorkerPath !== ''
|
||||||
|
@ -137,13 +142,9 @@ declare const __prefix: string;`
|
||||||
.filter((dirent) => dirent.isFile())
|
.filter((dirent) => dirent.isFile())
|
||||||
.map((dirent) => `/${dirent.name}`)
|
.map((dirent) => `/${dirent.name}`)
|
||||||
|
|
||||||
const _routes =
|
const _assets = Array.from(astroAssets.keys())
|
||||||
routes
|
.filter((key) => !key.includes('[...slug]'))
|
||||||
.filter(({ isIndex }) => isIndex)
|
.flatMap((key) => (key === '/' ? key : [key, `${key}/`]))
|
||||||
.flatMap(({ pathname }) =>
|
|
||||||
pathname === '/' ? pathname : [pathname, `${pathname}/`]
|
|
||||||
)
|
|
||||||
.filter((pathname) => pathname !== '') ?? []
|
|
||||||
|
|
||||||
const _pages =
|
const _pages =
|
||||||
pages
|
pages
|
||||||
|
@ -167,10 +168,10 @@ declare const __prefix: string;`
|
||||||
...excludeRoutes.map((route) => `${route}/`),
|
...excludeRoutes.map((route) => `${route}/`),
|
||||||
]
|
]
|
||||||
|
|
||||||
assets = [
|
const __assets = [
|
||||||
...new Set([
|
...new Set([
|
||||||
...assets,
|
...manifestAssets,
|
||||||
..._routes,
|
..._assets,
|
||||||
..._pages,
|
..._pages,
|
||||||
..._pagesWithoutEndSlash,
|
..._pagesWithoutEndSlash,
|
||||||
...customRoutes,
|
...customRoutes,
|
||||||
|
@ -187,17 +188,17 @@ declare const __prefix: string;`
|
||||||
|
|
||||||
if (logAssets) {
|
if (logAssets) {
|
||||||
logger.info(
|
logger.info(
|
||||||
`${assets.length} assets for caching: \n ▶ ${assets.toString().replaceAll(',', '\n ▶ ')}\n`
|
`${__assets.length} assets for caching: \n ▶ ${__assets.toString().replaceAll(',', '\n ▶ ')}\n`
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
logger.info(`${assets.length} assets for caching.`)
|
logger.info(`${__assets.length} assets for caching.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.info(`Using service worker in path: ${swPath}`)
|
logger.info(`Using service worker in path: ${swPath}`)
|
||||||
originalScript = await readFile(swPath)
|
originalScript = await readFile(swPath)
|
||||||
} catch {
|
} catch (err) {
|
||||||
logger.error(`Service worker script not found! ${swPath}`)
|
logger.error(err)
|
||||||
if (!swPath) {
|
if (!swPath) {
|
||||||
logger.error(`
|
logger.error(`
|
||||||
|
|
||||||
|
@ -207,7 +208,7 @@ declare const __prefix: string;`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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`
|
||||||
const prefixDeclaration = `const __prefix = ${JSON.stringify(assetCachePrefix)};\n`
|
const prefixDeclaration = `const __prefix = ${JSON.stringify(assetCachePrefix)};\n`
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@ayco/astro-sw",
|
"name": "@ayco/astro-sw",
|
||||||
"version": "0.8.14",
|
"version": "0.9.0",
|
||||||
"description": "Use your own authored service worker with Astro",
|
"description": "Use your own authored service worker with Astro",
|
||||||
"homepage": "https://ayco.io/n/@ayco/astro-sw",
|
"homepage": "https://ayco.io/n/@ayco/astro-sw",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
@ -13,8 +13,7 @@
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"astro-sw.js",
|
"astro-sw.js",
|
||||||
"globals.js",
|
"globals.js"
|
||||||
"strategies/*"
|
|
||||||
],
|
],
|
||||||
"main": "./astro-sw.js",
|
"main": "./astro-sw.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
@ -22,7 +21,10 @@
|
||||||
"node": ">=18.0.0"
|
"node": ">=18.0.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "vitest run"
|
"test": "vitest run",
|
||||||
|
"version:patch": "npm version patch",
|
||||||
|
"version:minor": "npm version minor",
|
||||||
|
"version:major": "npm version major"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"withastro",
|
"withastro",
|
||||||
|
@ -33,7 +35,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"esbuild": "^0.25.2"
|
"esbuild": "^0.25.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"peerDependencies": {
|
||||||
"astro": "^5.6.1"
|
"astro": "^5.6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue