Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add generateSW allowlist experimental option #23

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions examples/pwa-simple/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { defineConfig } from 'vitepress'
import { withPwa } from '@vite-pwa/vitepress'

const base = '/' // '/vite-plugin-pwa/'

export default withPwa(defineConfig({
vite: {
logLevel: 'info',
define: {
__DATE__: `'${new Date().toISOString()}'`,
},
},
base,
lang: 'en-US',
title: 'VitePress PWA',
description: 'Vite Plugin PWA Integration example for VitePress',
Expand All @@ -33,8 +36,6 @@ export default withPwa(defineConfig({
},
pwa: {
mode: 'development',
base: '/',
scope: '/',
registerType: 'autoUpdate',
// injectRegister: 'inline',
includeAssets: ['favicon.svg'],
Expand Down Expand Up @@ -64,6 +65,9 @@ export default withPwa(defineConfig({
workbox: {
globPatterns: ['**/*.{css,js,html,svg,png,ico,txt,woff2}'],
},
experimental: {
includeAllowlist: true,
},
devOptions: {
enabled: true,
suppressWarnings: true,
Expand Down
38 changes: 37 additions & 1 deletion src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,45 @@ export function withUserConfig<T = DefaultTheme.Config>(config: UserConfig<T>) {
return head
}

let allowlist: RegExp[] | undefined

if (pwa.strategies !== 'injectManifest' && pwa.experimental?.includeAllowlist === true) {
pwa.workbox = pwa.workbox ?? {}
// luckily, navigateFallbackAllowlist is a reference, so we can update it before generating SW
allowlist = pwa.workbox.navigateFallbackAllowlist = pwa.workbox.navigateFallbackAllowlist ?? []
pwa.workbox.runtimeCaching = pwa.workbox.runtimeCaching ?? []
// add offline support: without this, missing page will not work offline
pwa.workbox.runtimeCaching.push({
urlPattern: ({ request, sameOrigin }) => {
return sameOrigin && request.mode === 'navigate'
},
handler: 'NetworkOnly',
options: {
plugins: [{
/* this callback will be called when the fetch call fails */
handlerDidError: async () => Response.redirect('404', 302),
/* this callback will prevent caching the response */
cacheWillUpdate: async () => null,
}],
},
})
}

vitePressConfig.buildEnd = async (siteConfig) => {
await userBuildEnd?.(siteConfig)
api && !api.disabled && await api.generateSW()
if (api && !api.disabled) {
// add pages to allowlist: any page that is not in the allowlist will not work offline
if (typeof allowlist !== 'undefined') {
const base = siteConfig.site.base ?? '/'
for (const page of siteConfig.pages) {
if (page === 'index.md')
allowlist.push(new RegExp(`^${base}(.html)?$`))
else
allowlist.push(new RegExp(`^${base}${page.replace(/\.md$/, '(.html)?')}$`))
}
}
await api.generateSW()
}
}

return vitePressConfig
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import type { VitePWAOptions } from 'vite-plugin-pwa'

export interface PwaOptions extends Partial<VitePWAOptions> {
experimental?: {
/**
* When using `generateSW` strategy, include the logic to handle the `workbox.navigateFallbackAllowlist` option.
*
* @see https://github.com/vite-pwa/vitepress/issues/22
*
* @default false
*/
includeAllowlist?: boolean
}
}

declare module 'vitepress' {
Expand Down