chore(pwa): change sw generation logic (#2494)

This commit is contained in:
Joaquín Sánchez 2023-12-01 18:28:31 +01:00 committed by GitHub
parent 21376e013a
commit 5503ecbea2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 29 deletions

View file

@ -15,16 +15,6 @@ export const pwa: VitePWANuxtOptions = {
injectManifest: { injectManifest: {
globPatterns: ['**/*.{js,json,css,html,txt,svg,png,ico,webp,woff,woff2,ttf,eot,otf,wasm}'], globPatterns: ['**/*.{js,json,css,html,txt,svg,png,ico,webp,woff,woff2,ttf,eot,otf,wasm}'],
globIgnores: ['emojis/**', 'shiki/**', 'manifest**.webmanifest'], globIgnores: ['emojis/**', 'shiki/**', 'manifest**.webmanifest'],
manifestTransforms: [(entries) => {
const manifest = entries.map((entry) => {
if (entry.url.length > 1 && entry.url[0] !== '/')
entry.url = `/${entry.url}`
return entry
})
return { manifest, warnings: [] }
}],
}, },
devOptions: { devOptions: {
enabled: process.env.VITE_DEV_PWA === 'true', enabled: process.env.VITE_DEV_PWA === 'true',

View file

@ -32,25 +32,37 @@ export function configurePWAOptions(options: Partial<VitePWAOptions>, nuxt: Nuxt
options.devOptions.navigateFallbackAllowlist = [new RegExp(nuxt.options.app.baseURL) ?? /\//] options.devOptions.navigateFallbackAllowlist = [new RegExp(nuxt.options.app.baseURL) ?? /\//]
} }
config = options.workbox config = options.workbox
// todo: change navigateFallback based on the command: use 404 only when using generate
/* else if (nuxt.options.build) {
if (!options.workbox.navigateFallback)
options.workbox.navigateFallback = '/200.html'
} */
} }
if (!nuxt.options.dev) let buildAssetsDir = nuxt.options.app.buildAssetsDir ?? '_nuxt/'
config.manifestTransforms = [createManifestTransform(nuxt.options.app.baseURL ?? '/')] if (buildAssetsDir[0] === '/')
buildAssetsDir = buildAssetsDir.slice(1)
if (buildAssetsDir[buildAssetsDir.length - 1] !== '/')
buildAssetsDir += '/'
// Vite 5 support: allow override dontCacheBustURLsMatching
if (!('dontCacheBustURLsMatching' in config))
config.dontCacheBustURLsMatching = new RegExp(buildAssetsDir)
// handle payload extraction
if (nuxt.options.experimental.payloadExtraction) {
config.globPatterns = config.globPatterns ?? []
config.globPatterns.push('**/_payload.json')
} }
function createManifestTransform(base: string): import('workbox-build').ManifestTransform { // handle Nuxt App Manifest
let appManifestFolder: string | undefined
if (nuxt.options.experimental.appManifest) {
config.globPatterns = config.globPatterns ?? []
appManifestFolder = `${buildAssetsDir}builds/`
config.globPatterns.push(`${appManifestFolder}**/*.json`)
}
if (!nuxt.options.dev)
config.manifestTransforms = [createManifestTransform(nuxt.options.app.baseURL ?? '/', appManifestFolder)]
}
function createManifestTransform(base: string, appManifestFolder?: string): import('workbox-build').ManifestTransform {
return async (entries) => { return async (entries) => {
// prefix non html assets with base
/*
entries.filter(e => e && !e.url.endsWith('.html')).forEach((e) => {
if (!e.url.startsWith(base))
e.url = `${base}${e.url}`
})
*/
entries.filter(e => e && e.url.endsWith('.html')).forEach((e) => { entries.filter(e => e && e.url.endsWith('.html')).forEach((e) => {
const url = e.url.startsWith('/') ? e.url.slice(1) : e.url const url = e.url.startsWith('/') ? e.url.slice(1) : e.url
if (url === 'index.html') { if (url === 'index.html') {
@ -59,11 +71,19 @@ function createManifestTransform(base: string): import('workbox-build').Manifest
else { else {
const parts = url.split('/') const parts = url.split('/')
parts[parts.length - 1] = parts[parts.length - 1].replace(/\.html$/, '') parts[parts.length - 1] = parts[parts.length - 1].replace(/\.html$/, '')
// e.url = `${base}${parts.length > 1 ? parts.slice(0, parts.length - 1).join('/') : parts[0]}`
e.url = parts.length > 1 ? parts.slice(0, parts.length - 1).join('/') : parts[0] e.url = parts.length > 1 ? parts.slice(0, parts.length - 1).join('/') : parts[0]
} }
}) })
if (appManifestFolder) {
const regExp = /(\/)?[0-9a-f]{8}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{4}\b-[0-9a-f]{12}\.json$/i
// we need to remove the revision from the sw prechaing manifest, UUID is enough:
// we don't use dontCacheBustURLsMatching, single regex
entries.filter(e => e && e.url.startsWith(appManifestFolder) && regExp.test(e.url)).forEach((e) => {
e.revision = null
})
}
return { manifest: entries, warnings: [] } return { manifest: entries, warnings: [] }
} }
} }

View file

@ -217,11 +217,9 @@ export default defineNuxtModule<VitePWANuxtOptions>({
} }
} }
}) })
nuxt.hook('nitro:init', (nitro) => { nuxt.hook('nitro:build:public-assets', async () => {
nitro.hooks.hook('rollup:before', async () => {
await resolveVitePluginPWAAPI()?.generateSW() await resolveVitePluginPWAAPI()?.generateSW()
}) })
})
} }
}, },
}) })