feat: use unjs/pathe & extract public files for caching
This commit is contained in:
parent
9986976b38
commit
a04cae6da8
4 changed files with 20 additions and 6 deletions
|
@ -13,7 +13,7 @@ export default defineConfig({
|
|||
path: "./example_sw.js",
|
||||
assetCachePrefix: 'cozy-reader',
|
||||
customRoutes: [
|
||||
'/treads'
|
||||
'/threads'
|
||||
]
|
||||
})
|
||||
]
|
||||
|
|
13
index.js
13
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);
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@astrojs/node": "^8.3.3",
|
||||
"astro": "^4.14.2"
|
||||
"astro": "^4.14.2",
|
||||
"pathe": "^1.1.2"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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: {}
|
||||
|
|
Loading…
Reference in a new issue