Skip to content

Commit

Permalink
feat: add support for 'script-defer' injectRegister option (#27)
Browse files Browse the repository at this point in the history
* feat: add support for 'script-defer' option for injectRegister script

* chore: use `script-defer` in pwa-simple example
  • Loading branch information
userquin committed Nov 25, 2023
1 parent c6838f7 commit 104dbf1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/pwa-simple/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default withPwa(defineConfig({
pwa: {
mode: 'development',
registerType: 'autoUpdate',
// injectRegister: 'inline',
injectRegister: 'script-defer',
includeAssets: ['favicon.svg'],
manifest: {
name: 'VitePress PWA',
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@vite-pwa/vitepress",
"type": "module",
"version": "0.3.0",
"packageManager": "pnpm@8.10.5",
"packageManager": "pnpm@8.11.0",
"description": "Zero-config PWA for VitePress",
"author": "antfu <anthonyfu117@hotmail.com>",
"license": "MIT",
Expand Down Expand Up @@ -42,7 +42,7 @@
"release": "bumpp && npm publish"
},
"peerDependencies": {
"vite-plugin-pwa": ">=0.17.0 <1"
"vite-plugin-pwa": ">=0.17.2 <1"
},
"devDependencies": {
"@antfu/eslint-config": "^0.43.1",
Expand All @@ -55,7 +55,7 @@
"typescript": "^5.2.2",
"unbuild": "^2.0.0",
"vite": "^5.0.0",
"vite-plugin-pwa": ">=0.17.0 <1",
"vite-plugin-pwa": ">=0.17.2 <1",
"vitepress": "1.0.0-rc.28"
},
"pnpm": {
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function configurePWAOptions<T = DefaultTheme.Config>(config: UserConfig<
else {
pwa.workbox = pwa.workbox ?? {}
pwa.workbox.dontCacheBustURLsMatching = dontCacheBustURLsMatching
if (pwa.registerType === 'autoUpdate' && (pwa.injectRegister === 'script' || pwa.injectRegister === 'inline')) {
if (pwa.registerType === 'autoUpdate' && (pwa.injectRegister === 'script' || pwa.injectRegister === 'script-defer' || pwa.injectRegister === 'inline')) {
pwa.workbox.clientsClaim = true
pwa.workbox.skipWaiting = true
}
Expand Down
28 changes: 20 additions & 8 deletions src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,33 @@ export function withUserConfig<T = DefaultTheme.Config>(config: UserConfig<T>) {

const registerSWData = api?.registerSWData()
if (registerSWData && registerSWData.shouldRegisterSW) {
if (registerSWData.inline) {
if (registerSWData.mode === 'inline') {
head.push([
'script',
{ id: 'vite-plugin-pwa:inline-sw' },
`if('serviceWorker' in navigator) {window.addEventListener('load', () => {navigator.serviceWorker.register('${registerSWData.inlinePath}', { scope: '${registerSWData.scope}' })})}`,
])
}
else {
head.push([
'script',
{
id: 'vite-plugin-pwa:register-sw',
src: registerSWData.registerPath,
},
])
if (registerSWData.mode === 'script-defer') {
head.push([
'script',
{
id: 'vite-plugin-pwa:register-sw',
defer: 'defer',
src: registerSWData.registerPath,
},
])
}
else {
head.push([
'script',
{
id: 'vite-plugin-pwa:register-sw',
src: registerSWData.registerPath,
},
])
}
}
}

Expand Down

0 comments on commit 104dbf1

Please sign in to comment.