feat: use unjs/pathe & extract public files for caching

This commit is contained in:
Ayo Ayco 2024-08-15 21:09:45 +02:00
parent 9986976b38
commit a04cae6da8
4 changed files with 20 additions and 6 deletions

View file

@ -13,7 +13,7 @@ export default defineConfig({
path: "./example_sw.js", path: "./example_sw.js",
assetCachePrefix: 'cozy-reader', assetCachePrefix: 'cozy-reader',
customRoutes: [ customRoutes: [
'/treads' '/threads'
] ]
}) })
] ]

View file

@ -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 { fileURLToPath } from 'node:url';
import path from 'node:path'; import path from 'pathe';
import { randomUUID } from "node:crypto"; import { randomUUID } from "node:crypto";
/** /**
@ -83,6 +83,10 @@ export default function serviceWorker(config) {
const swPath = path.join(__dirname, serviceWorkerPath ?? ''); const swPath = path.join(__dirname, serviceWorkerPath ?? '');
let originalScript; let originalScript;
const _publicFiles = (await readdir(dir, {withFileTypes: true}) ?? [])
.filter(dirent => dirent.isFile())
.map(dirent => `/${dirent.name}`);
const _routes = routes const _routes = routes
.filter(({isIndex}) => isIndex) .filter(({isIndex}) => isIndex)
.map(({pathname}) => pathname) .map(({pathname}) => pathname)
@ -111,9 +115,10 @@ export default function serviceWorker(config) {
..._routes, ..._routes,
..._pages, ..._pages,
..._pagesWithoutEndSlash, ..._pagesWithoutEndSlash,
...customRoutes ...customRoutes,
..._publicFiles
])] ])]
.filter(asset => !asset.includes('404')); .filter(asset => !!asset && asset !== '' && !asset.includes('404'));
console.log('[astro-sw] Assets for caching:', assets); console.log('[astro-sw] Assets for caching:', assets);

View file

@ -30,6 +30,7 @@
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@astrojs/node": "^8.3.3", "@astrojs/node": "^8.3.3",
"astro": "^4.14.2" "astro": "^4.14.2",
"pathe": "^1.1.2"
} }
} }

View file

@ -14,6 +14,9 @@ importers:
astro: astro:
specifier: ^4.14.2 specifier: ^4.14.2
version: 4.14.2(rollup@4.20.0)(typescript@5.5.4) version: 4.14.2(rollup@4.20.0)(typescript@5.5.4)
pathe:
specifier: ^1.1.2
version: 1.1.2
packages: packages:
@ -1319,6 +1322,9 @@ packages:
path-to-regexp@6.2.2: path-to-regexp@6.2.2:
resolution: {integrity: sha512-GQX3SSMokngb36+whdpRXE+3f9V8UzyAorlYvOGx87ufGHehNTn5lCxrKtLyZ4Yl/wEKnNnr98ZzOwwDZV5ogw==} resolution: {integrity: sha512-GQX3SSMokngb36+whdpRXE+3f9V8UzyAorlYvOGx87ufGHehNTn5lCxrKtLyZ4Yl/wEKnNnr98ZzOwwDZV5ogw==}
pathe@1.1.2:
resolution: {integrity: sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==}
picocolors@1.0.1: picocolors@1.0.1:
resolution: {integrity: sha512-anP1Z8qwhkbmu7MFP5iTt+wQKXgwzf7zTyGlcdzabySa9vd0Xt392U0rVmz9poOaBj0uHJKyyo9/upk0HrEQew==} resolution: {integrity: sha512-anP1Z8qwhkbmu7MFP5iTt+wQKXgwzf7zTyGlcdzabySa9vd0Xt392U0rVmz9poOaBj0uHJKyyo9/upk0HrEQew==}
@ -3182,6 +3188,8 @@ snapshots:
path-to-regexp@6.2.2: {} path-to-regexp@6.2.2: {}
pathe@1.1.2: {}
picocolors@1.0.1: {} picocolors@1.0.1: {}
picomatch@2.3.1: {} picomatch@2.3.1: {}