Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow disabling all precaching #454

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: allow disabling all precaching
  • Loading branch information
rinu committed Apr 20, 2021
commit 4fa642fadeec31a7754e6d21bdabbbc284fef4e7
39 changes: 21 additions & 18 deletions src/workbox/options.ts
Original file line number Diff line number Diff line change
@@ -67,34 +67,37 @@ 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
if (options.cacheOptions.cacheId === undefined) {
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',
2 changes: 1 addition & 1 deletion templates/workbox/sw.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion types/workbox.d.ts
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ export interface WorkboxOptions {
/**
* Default: `[]`
*/
preCaching: string[] | {url: string, revision: string}[],
preCaching: false | string[] | {url: string, revision: string}[],
cacheOptions: {
/**
* Default: `<npm package name> || nuxt`