Replies: 2 comments
-
I have it working, I had an errata on the astro.config.mjs// astro.config.mjs
export default defineConfig({
integrations: [
{
name: 'vite-plugin-pwa:astro:hook',
hooks: {
'astro:config:done': (options) => {
const vite = options.config.vite
distFolder = vite.build.outDir ?? 'dist'
for (const p of vite.plugins) {
if (Array.isArray(p)) {
pwaPlugin = p.find(p1 => p1.name === 'vite-plugin-pwa')
break
}
}
},
'astro:build:done': async ({ routes }) => {
if (routes && pwaPlugin && pwaPlugin.api && !pwaPlugin.api.isDisabled()) {
console.log(distFolder)
console.log(routes.map(r => `${r.component.slice(9)}`))
const addRoutes = await Promise.all(routes.filter(r => r.type === 'page').map(r => {
let path = r.component.slice(9, r.component.lastIndexOf('.'))
// todo@userquin: find the correct mapping
path = path === '/index' ? '/index.html' : (path === r.pathname ? `${path}/index.html` : `${path}/`)
console.log(`${path} => ${r.pathname}`)
return buildManifestEntry(r.pathname, path.slice(1))
}))
pwaPlugin.api.extendManifestEntries((manifestEntries) => {
manifestEntries.push(...addRoutes)
})
await pwaPlugin.api.generateSW()
}
},
},
}
],
vite: {
build: {
sourcemap: process.env.SOURCE_MAP === 'true',
},
plugins: [
VitePWA(pwaOptions),
replace(replaceOptions),
],
},
}); |
Beta Was this translation helpful? Give feedback.
0 replies
-
Previous code is only for auto update strategy, to use prompt for update we need to use a framework. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to add built-in support on
vite-plugin-pwa
using the Astro Integration API via hooks and I need thevite
entry configured to access thevite-plugin-pwa
's inter-plugin communication api to regenerate the pwaservice worker
to add the generated pages to theservive worker
manifest and so allow work offline.It would be great if the
astro:build:done
hook exposes theVite
used on the build. Right now I can run a post build script but I need to loadVite
again to access the pwa plugin and regenerate theservice worker
entry point (self.__WB_MANIFEST
).The
astro:build:done
hook only contains the router names, it would be also great if the path to locate the html file can be included.Beta Was this translation helpful? Give feedback.
All reactions