Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
fix(workbox): fix worker when using pages folder
Browse files Browse the repository at this point in the history
Fixes #11
  • Loading branch information
kevinmarrec committed Jun 7, 2022
1 parent aa01db1 commit 52ecdb9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
3 changes: 3 additions & 0 deletions example/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export default defineNuxtConfig({
meta: {
// Generate splash screens for iOS
mobileAppIOS: true
},
workbox: {
enabled: true
}
},
unocss: {
Expand Down
File renamed without changes.
14 changes: 0 additions & 14 deletions src/runtime/workbox/plugin.ts

This file was deleted.

17 changes: 11 additions & 6 deletions src/workbox.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { join } from 'pathe'
import { addPlugin, addTemplate, useNuxt } from '@nuxt/kit'
import { addTemplate, useNuxt } from '@nuxt/kit'
import consola from 'consola'
import { join } from 'pathe'
import { joinURL } from 'ufo'
import type { PWAContext } from './types'

export default (pwa: PWAContext) => {
if (!pwa.workbox || !pwa.workbox.enabled) { return }

const options = pwa.workbox
const nuxt = useNuxt()
const head = nuxt.options.app.head as Required<typeof nuxt.options.app.head>

// Warning when in development mode
if (nuxt.options.dev) {
Expand All @@ -27,9 +29,12 @@ export default (pwa: PWAContext) => {
options
})

// Plugin that registers the Service Worker
addPlugin({
src: pwa._resolver.resolve('./runtime/workbox/plugin'),
mode: 'client'
// Embed script that registers the Service Worker
head.script.push({
children: [
"if ('serviceWorker' in navigator) {",
` window.addEventListener('load', () => navigator.serviceWorker.register('${joinURL(nuxt.options.app.baseURL, 'sw.js')}'))`,
'}'
].join('\n')
})
}

0 comments on commit 52ecdb9

Please sign in to comment.