diff --git a/astro.config.mjs b/astro.config.mjs index 431718e..07b0938 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -3,10 +3,11 @@ import node from "@astrojs/node"; import serviceWorker from "./index.js"; export default defineConfig({ - output: "hybrid", - adapter: node({ - mode: "middleware" - }), + // output: "server", + // adapter: node({ + // mode: "middleware" + // }), + site: 'https://ayo.ayco.io', integrations: [ serviceWorker({ path: "./example_sw.js", diff --git a/index.js b/index.js index cc3fe0e..971cfd8 100644 --- a/index.js +++ b/index.js @@ -70,14 +70,22 @@ export default function serviceWorker(config) { registerSW();` + let output = 'static' + return { 'name': 'astro-sw', 'hooks': { - 'astro:config:setup': ({injectScript}) => { + 'astro:config:setup': ({injectScript, config}) => { + output = config.output; injectScript('page', registrationScript); }, 'astro:build:ssr': ({ manifest }) => { - assets = manifest.assets.filter(ass => !ass.includes('sw.js')) + const files = manifest.routes.map(route => route.file.replaceAll('/', '')); + const assetsMinusFiles = manifest.assets.filter(ass => !files.includes(ass.replaceAll('/', ''))); + + assets = output === 'static' + ? assetsMinusFiles + : manifest.assets.filter(ass => !ass.includes('sw.js')); }, 'astro:build:done': async ({ dir, routes, pages, }) => { const outFile = fileURLToPath(new URL('./sw.js', dir)); @@ -92,11 +100,12 @@ export default function serviceWorker(config) { ?? []; const _pages = pages - .map(({pathname}) => pathname) - .filter(pathname => pathname !== '') + .filter(({pathname}) => pathname !== '') + .map(({pathname}) => `/${pathname}`) ?? []; const _pagesWithoutEndSlash = pages + .filter(({pathname}) => pathname !== '') .map(({pathname}) => { const lastChar = pathname.slice(-1); const len = pathname.length; @@ -108,6 +117,7 @@ export default function serviceWorker(config) { ?? []; assets = [...new Set([...assets, ..._routes, ..._pages, ..._pagesWithoutEndSlash])] + console.log('>>> assets', assets); try { console.log('[astro-sw] Using service worker:', swPath);