feat: don't include index.html

This commit is contained in:
Ayo Ayco 2024-08-15 21:42:00 +02:00
parent 93eaec2a38
commit 0e29dfbf15

View file

@ -65,7 +65,7 @@ export default function serviceWorker(config) {
return { return {
'name': 'astro-sw', 'name': 'astro-sw',
'hooks': { 'hooks': {
'astro:config:setup': ({injectScript, config}) => { 'astro:config:setup': ({ injectScript, config }) => {
output = config.output; output = config.output;
injectScript('page', registrationScript); injectScript('page', registrationScript);
}, },
@ -77,48 +77,50 @@ export default function serviceWorker(config) {
? assetsMinusFiles ? assetsMinusFiles
: manifest.assets.filter(ass => !ass.includes('sw.js')); : manifest.assets.filter(ass => !ass.includes('sw.js'));
}, },
'astro:build:done': async ({ dir, routes, pages, }) => { 'astro:build:done': async ({ dir, routes, pages, }) => {
const outFile = fileURLToPath(new URL('./sw.js', dir)); const outFile = fileURLToPath(new URL('./sw.js', dir));
const __dirname = path.resolve(path.dirname('.')); const __dirname = path.resolve(path.dirname('.'));
const swPath = path.join(__dirname, serviceWorkerPath ?? ''); const swPath = path.join(__dirname, serviceWorkerPath ?? '');
let originalScript; let originalScript;
const _publicFiles = (await readdir(dir, {withFileTypes: true}) ?? []) const _publicFiles = (await readdir(dir, { withFileTypes: true }) ?? [])
.filter(dirent => dirent.isFile()) .filter(dirent => dirent.isFile())
.map(dirent => `/${dirent.name}`); .map(dirent => `/${dirent.name}`);
const _routes = routes const _routes = routes
.filter(({isIndex}) => isIndex) .filter(({ isIndex }) => isIndex)
.map(({pathname}) => pathname) .map(({ pathname }) => pathname)
.filter(pathname => pathname !== '') .filter(pathname => pathname !== '')
?? []; ?? [];
const _pages = pages const _pages = pages
.filter(({pathname}) => pathname !== '') .filter(({ pathname }) => pathname !== '')
.map(({pathname}) => `/${pathname}`) .map(({ pathname }) => `/${pathname}`)
?? []; ?? [];
const _pagesWithoutEndSlash = pages const _pagesWithoutEndSlash = pages
.filter(({pathname}) => pathname !== '') .filter(({ pathname }) => pathname !== '')
.map(({pathname}) => { .map(({ pathname }) => {
const lastChar = pathname.slice(-1); const lastChar = pathname.slice(-1);
const len = pathname.length; const len = pathname.length;
return lastChar === '/' return lastChar === '/'
? `/${pathname.slice(0, len-1)}` ? `/${pathname.slice(0, len - 1)}`
: `/${pathname}`; : `/${pathname}`;
}) })
.filter(pathname => pathname !== '') .filter(pathname => pathname !== '')
?? []; ?? [];
assets = [...new Set([ assets = [...new Set([
...assets, ...assets,
..._routes, ..._routes,
..._pages, ..._pages,
..._pagesWithoutEndSlash, ..._pagesWithoutEndSlash,
...customRoutes, ...customRoutes,
..._publicFiles ..._publicFiles
])] ])].filter(asset => !!asset
.filter(asset => !!asset && asset !== '' && !asset.includes('404')); && asset !== ''
&& !asset.includes('404')
&& !asset.includes('index.html'));
console.log('[astro-sw] Assets for caching:', assets); console.log('[astro-sw] Assets for caching:', assets);