diff --git a/test/__snapshots__/pwa.test.js.snap b/test/__snapshots__/pwa.test.js.snap index 529a63df..805781fb 100644 --- a/test/__snapshots__/pwa.test.js.snap +++ b/test/__snapshots__/pwa.test.js.snap @@ -83,67 +83,94 @@ Array [ `; exports[`pwa sw.js 1`] = ` -"importScripts('https://cdn.jsdelivr.net/npm/workbox-cdn/workbox/workbox-sw.js', 'custom-sw.js') - -// -------------------------------------------------- -// Configure -// -------------------------------------------------- - -// Set workbox config -workbox.setConfig({ - \\"debug\\": true -}) - -// Set workbox cache names -workbox.core.setCacheNameDetails({ - \\"prefix\\": \\"test\\", - \\"googleAnalytics\\": \\"test-ga\\" -}) - -// Start controlling any existing clients as soon as it activates -workbox.core.clientsClaim() - -// Skip over the SW waiting lifecycle stage -workbox.core.skipWaiting() - -workbox.precaching.cleanupOutdatedCaches() - -// Enable offline Google Analytics tracking -workbox.googleAnalytics.initialize() - -// -- Start of workboxExtensions -- -// Workbox Extension -// -- End of workboxExtensions -- - -// -------------------------------------------------- -// Precaches -// -------------------------------------------------- - -// Precache assets - -workbox.precaching.precacheAndRoute([ - \\"precache.js\\" -], { - \\"cacheId\\": \\"@nuxtjs/pwa-prod\\", - \\"directoryIndex\\": \\"/\\" -}) - -// -- Start of cachingExtensions -- -// Caching Extension -// -- End of cachingExtensions -- - -// -------------------------------------------------- -// Runtime Caching -// -------------------------------------------------- - -// Register route handlers for runtimeCaching -workbox.routing.registerRoute(new RegExp('https://google.com/.*'), new workbox.strategies.CacheFirst(), 'GET') -workbox.routing.registerRoute(new RegExp('https://pwa.nuxtjs.org/.*'), new workbox.strategies.CacheFirst({plugins:[new workbox.expiration.ExpirationPlugin({maxEntries:10,maxAgeSeconds:300})],cacheName:'nuxt-pwa'}), 'GET') -workbox.routing.registerRoute(new RegExp('/_nuxt/'), new workbox.strategies.CacheFirst(), 'GET') -workbox.routing.registerRoute(new RegExp('/'), new workbox.strategies.NetworkFirst(), 'GET') - -// -- Start of routingExtensions -- -// Routing Extension -// -- End of routingExtensions -- +"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} + +importScripts(...[options.workboxURL, ...options.importScripts]) + +initWorkbox(workbox, options) +workboxExtensions(workbox, options) +precacheAssets(workbox, options) +cachingExtensions(workbox, options) +runtimeCaching(workbox, options) +offlinePage(workbox, options) +routingExtensions(workbox, options) + +function getProp(obj, prop) { + return prop.split('.').reduce((p, c) => p[c], obj) +} + +function initWorkbox(workbox, options) { + if (options.config) { + // Set workbox config + workbox.setConfig(options.config) + } + + if (options.cacheNames) { + // Set workbox cache names + workbox.core.setCacheNameDetails(options.cacheNames) + } + + if (options.clientsClaim) { + // Start controlling any existing clients as soon as it activates + workbox.core.clientsClaim() + } + + if (options.skipWaiting) { + workbox.core.skipWaiting() + } + + if (options.cleanupOutdatedCaches) { + workbox.precaching.cleanupOutdatedCaches() + } + + if (options.offlineAnalytics) { + // Enable offline Google Analytics tracking + workbox.googleAnalytics.initialize() + } +} + +function precacheAssets(workbox, options) { + if (options.preCaching.length) { + workbox.precaching.precacheAndRoute(options.preCaching, options.cacheOptions) + } +} + +function runtimeCaching(workbox, options) { + for (const entry of options.runtimeCaching) { + const urlPattern = new RegExp(entry.urlPattern) + const method = entry.method || 'GET' + + const plugins = (entry.strategyPlugins || []) + .map(p => new (getProp(workbox, p.use))(...p.config)) + + const strategyOptions = { ...entry.strategyOptions, plugins } + + const strategy = new workbox.strategies[entry.handler](strategyOptions) + + workbox.routing.registerRoute(urlPattern, strategy, method) + } +} + +function offlinePage(workbox, options) { + // Register router handler for offlinePage + workbox.routing.registerRoute(new RegExp(options.pagesURLPattern), ({ request, event }) => { + const strategy = new workbox.strategies[options.offlineStrategy] + return strategy + .handle({ request, event }) + .catch(() => caches.match(options.offlinePage)) + }) +} + +function workboxExtensions(workbox, options) { + // Workbox Extension +} + +function cachingExtensions(workbox, options) { + // Caching Extension +} + +function routingExtensions(workbox, options) { + // Routing Extension +} " `;