diff --git a/astro.config.mjs b/astro.config.mjs index a09980d..a685145 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -13,7 +13,7 @@ export default defineConfig({ path: "./example_sw.js", assetCachePrefix: 'cozy-reader', customRoutes: [ - '/treads' + '/threads' ] }) ] diff --git a/index.js b/index.js index 97cbf86..352b8c2 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ -import { readFile, writeFile } from 'node:fs/promises'; +import { readFile, writeFile, readdir } from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; -import path from 'node:path'; +import path from 'pathe'; import { randomUUID } from "node:crypto"; /** @@ -83,6 +83,10 @@ export default function serviceWorker(config) { const swPath = path.join(__dirname, serviceWorkerPath ?? ''); let originalScript; + const _publicFiles = (await readdir(dir, {withFileTypes: true}) ?? []) + .filter(dirent => dirent.isFile()) + .map(dirent => `/${dirent.name}`); + const _routes = routes .filter(({isIndex}) => isIndex) .map(({pathname}) => pathname) @@ -111,9 +115,10 @@ export default function serviceWorker(config) { ..._routes, ..._pages, ..._pagesWithoutEndSlash, - ...customRoutes + ...customRoutes, + ..._publicFiles ])] - .filter(asset => !asset.includes('404')); + .filter(asset => !!asset && asset !== '' && !asset.includes('404')); console.log('[astro-sw] Assets for caching:', assets); diff --git a/package.json b/package.json index 31436ea..532e431 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "license": "MIT", "devDependencies": { "@astrojs/node": "^8.3.3", - "astro": "^4.14.2" + "astro": "^4.14.2", + "pathe": "^1.1.2" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index db0f412..e58eef8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: astro: specifier: ^4.14.2 version: 4.14.2(rollup@4.20.0)(typescript@5.5.4) + pathe: + specifier: ^1.1.2 + version: 1.1.2 packages: @@ -1319,6 +1322,9 @@ packages: path-to-regexp@6.2.2: resolution: {integrity: sha512-GQX3SSMokngb36+whdpRXE+3f9V8UzyAorlYvOGx87ufGHehNTn5lCxrKtLyZ4Yl/wEKnNnr98ZzOwwDZV5ogw==} + pathe@1.1.2: + resolution: {integrity: sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==} + picocolors@1.0.1: resolution: {integrity: sha512-anP1Z8qwhkbmu7MFP5iTt+wQKXgwzf7zTyGlcdzabySa9vd0Xt392U0rVmz9poOaBj0uHJKyyo9/upk0HrEQew==} @@ -3182,6 +3188,8 @@ snapshots: path-to-regexp@6.2.2: {} + pathe@1.1.2: {} + picocolors@1.0.1: {} picomatch@2.3.1: {}