From 4fa642fadeec31a7754e6d21bdabbbc284fef4e7 Mon Sep 17 00:00:00 2001 From: Rauno Moisto Date: Tue, 20 Apr 2021 17:19:15 +0300 Subject: [PATCH] feat: allow disabling all precaching --- src/workbox/options.ts | 39 +++++++++++++++++++++------------------ templates/workbox/sw.js | 2 +- types/workbox.d.ts | 2 +- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/workbox/options.ts b/src/workbox/options.ts index 8d3b769a..41ff8d40 100644 --- a/src/workbox/options.ts +++ b/src/workbox/options.ts @@ -67,24 +67,30 @@ export function getOptions (nuxt, pwa: PWAContext): WorkboxOptions { if (!options.cacheOptions.revision) { options.cacheOptions.revision = randomString(12) } - const normalizePreCaching = (arr: any | any[]) => [].concat(arr).map(url => ({ - revision: options.cacheOptions.revision, - ...(typeof url === 'string' ? { url } : url) - })) - // Add start_url to precaching - if (pwa.manifest && pwa.manifest.start_url) { - options.preCaching.unshift(...normalizePreCaching(pwa.manifest.start_url)) - } + if (options.preCaching !== false) { + const normalizePreCaching = (arr: any | any[]) => [].concat(arr).map(url => ({ + revision: options.cacheOptions.revision, + ...(typeof url === 'string' ? { url } : url) + })) - // Add offlineAssets to precaching - if (options.offlineAssets.length) { - options.preCaching.unshift(...normalizePreCaching(options.offlineAssets)) - } + // Add start_url to precaching + if (pwa.manifest && pwa.manifest.start_url) { + options.preCaching.unshift(...normalizePreCaching(pwa.manifest.start_url)) + } + + // Add offlineAssets to precaching + if (options.offlineAssets.length) { + options.preCaching.unshift(...normalizePreCaching(options.offlineAssets)) + } + + // Add offlinePage to precaching + if (options.offlinePage) { + options.preCaching.unshift(...(normalizePreCaching(options.offlinePage))) + } - // Add offlinePage to precaching - if (options.offlinePage) { - options.preCaching.unshift(...(normalizePreCaching(options.offlinePage))) + // Normalize preCaching + options.preCaching = normalizePreCaching(options.preCaching) } // Default cacheId @@ -92,9 +98,6 @@ export function getOptions (nuxt, pwa: PWAContext): WorkboxOptions { options.cacheOptions.cacheId = (process.env.npm_package_name || 'nuxt') + (nuxt.options.dev ? '-dev' : '-prod') } - // Normalize preCaching - options.preCaching = normalizePreCaching(options.preCaching) - // Normalize runtimeCaching const pluginModules = { BackgroundSync: 'backgroundSync.BackgroundSyncPlugin', diff --git a/templates/workbox/sw.js b/templates/workbox/sw.js index 039fd0d7..325d4d85 100644 --- a/templates/workbox/sw.js +++ b/templates/workbox/sw.js @@ -45,7 +45,7 @@ function initWorkbox(workbox, options) { } function precacheAssets(workbox, options) { - if (options.preCaching.length) { + if (options.preCaching && options.preCaching.length) { workbox.precaching.precacheAndRoute(options.preCaching, options.cacheOptions) } } diff --git a/types/workbox.d.ts b/types/workbox.d.ts index a43d631b..9430bab8 100644 --- a/types/workbox.d.ts +++ b/types/workbox.d.ts @@ -69,7 +69,7 @@ export interface WorkboxOptions { /** * Default: `[]` */ - preCaching: string[] | {url: string, revision: string}[], + preCaching: false | string[] | {url: string, revision: string}[], cacheOptions: { /** * Default: ` || nuxt`