Skip to content

Commit 2b9856e

Browse files
committed
fix(workbox): handle offlinePage options (fixes #365)
1 parent 8d74330 commit 2b9856e

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

lib/workbox/module.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ module.exports = function nuxtWorkbox (pwa) {
4343
'offlineAnalytics',
4444
'preCaching',
4545
'runtimeCaching',
46-
'offlinePage'
46+
'offlinePage',
47+
'pagesURLPattern',
48+
'offlineStrategy'
4749
]),
4850
routingExtensions: await readJSFiles.call(this, options.routingExtensions),
4951
cachingExtensions: await readJSFiles.call(this, options.cachingExtensions),

lib/workbox/templates/sw.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ function runtimeCaching(workbox, options) {
6767
}
6868

6969
function offlinePage(workbox, options) {
70-
// Register router handler for offlinePage
71-
workbox.routing.registerRoute(new RegExp(options.pagesURLPattern), ({ request, event }) => {
72-
const strategy = new workbox.strategies[options.offlineStrategy]
73-
return strategy
74-
.handle({ request, event })
75-
.catch(() => caches.match(options.offlinePage))
76-
})
70+
if (options.offlinePage) {
71+
// Register router handler for offlinePage
72+
workbox.routing.registerRoute(new RegExp(options.pagesURLPattern), ({ request, event }) => {
73+
const strategy = new workbox.strategies[options.offlineStrategy]
74+
return strategy
75+
.handle({ request, event })
76+
.catch(() => caches.match(options.offlinePage))
77+
})
78+
}
7779
}
7880

7981
function workboxExtensions(workbox, options) {

test/__snapshots__/pwa.test.js.snap

+10-8
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Array [
8080
`;
8181

8282
exports[`pwa sw.js 1`] = `
83-
"const options = {\\"workboxURL\\":\\"https://cdn.jsdelivr.net/npm/workbox-cdn/workbox/workbox-sw.js\\",\\"importScripts\\":[\\"custom-sw.js\\"],\\"config\\":{\\"debug\\":true},\\"cacheNames\\":{\\"prefix\\":\\"test\\",\\"googleAnalytics\\":\\"test-ga\\"},\\"clientsClaim\\":true,\\"skipWaiting\\":true,\\"cleanupOutdatedCaches\\":true,\\"offlineAnalytics\\":true,\\"preCaching\\":[\\"precache.js\\"],\\"runtimeCaching\\":[{\\"urlPattern\\":\\"https://google.com/.*\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]},{\\"urlPattern\\":\\"https://pwa.nuxtjs.org/.*\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyOptions\\":{\\"cacheName\\":\\"nuxt-pwa\\"},\\"strategyPlugins\\":[{\\"use\\":\\"expiration.ExpirationPlugin\\",\\"config\\":[{\\"maxEntries\\":10,\\"maxAgeSeconds\\":300}]}]},{\\"urlPattern\\":\\"/_nuxt/\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]},{\\"urlPattern\\":\\"/\\",\\"handler\\":\\"NetworkFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]}],\\"offlinePage\\":null}
83+
"const options = {\\"workboxURL\\":\\"https://cdn.jsdelivr.net/npm/workbox-cdn/workbox/workbox-sw.js\\",\\"importScripts\\":[\\"custom-sw.js\\"],\\"config\\":{\\"debug\\":true},\\"cacheNames\\":{\\"prefix\\":\\"test\\",\\"googleAnalytics\\":\\"test-ga\\"},\\"clientsClaim\\":true,\\"skipWaiting\\":true,\\"cleanupOutdatedCaches\\":true,\\"offlineAnalytics\\":true,\\"preCaching\\":[\\"precache.js\\"],\\"runtimeCaching\\":[{\\"urlPattern\\":\\"https://google.com/.*\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]},{\\"urlPattern\\":\\"https://pwa.nuxtjs.org/.*\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyOptions\\":{\\"cacheName\\":\\"nuxt-pwa\\"},\\"strategyPlugins\\":[{\\"use\\":\\"expiration.ExpirationPlugin\\",\\"config\\":[{\\"maxEntries\\":10,\\"maxAgeSeconds\\":300}]}]},{\\"urlPattern\\":\\"/_nuxt/\\",\\"handler\\":\\"CacheFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]},{\\"urlPattern\\":\\"/\\",\\"handler\\":\\"NetworkFirst\\",\\"method\\":\\"GET\\",\\"strategyPlugins\\":[]}],\\"offlinePage\\":null,\\"pagesURLPattern\\":\\"/\\",\\"offlineStrategy\\":\\"NetworkFirst\\"}
8484
8585
importScripts(...[options.workboxURL, ...options.importScripts])
8686
@@ -149,13 +149,15 @@ function runtimeCaching(workbox, options) {
149149
}
150150
151151
function offlinePage(workbox, options) {
152-
// Register router handler for offlinePage
153-
workbox.routing.registerRoute(new RegExp(options.pagesURLPattern), ({ request, event }) => {
154-
const strategy = new workbox.strategies[options.offlineStrategy]
155-
return strategy
156-
.handle({ request, event })
157-
.catch(() => caches.match(options.offlinePage))
158-
})
152+
if (options.offlinePage) {
153+
// Register router handler for offlinePage
154+
workbox.routing.registerRoute(new RegExp(options.pagesURLPattern), ({ request, event }) => {
155+
const strategy = new workbox.strategies[options.offlineStrategy]
156+
return strategy
157+
.handle({ request, event })
158+
.catch(() => caches.match(options.offlinePage))
159+
})
160+
}
159161
}
160162
161163
function workboxExtensions(workbox, options) {

0 commit comments

Comments
 (0)