import { resolve } from 'path' ; import cloneDeep from 'clone-deep' ; import { joinUrl , getRouteParams , startCase , randomString , PKG_DIR } from '../utils' ; import type { WorkboxOptions , PWAContext } from '../../types' ; import { defaults } from './defaults' ; export function getOptions ( nuxt , pwa ) { const options = cloneDeep ( { ... defaults , ... pwa . workbox } ) ; if ( options . enabled === undefined ) { options . enabled = ! nuxt . options . dev || options . dev ; } if ( ! options . routerBase ) { options . routerBase = nuxt . options . router . base ; } if ( ! options . publicPath ) { const { publicPath } = getRouteParams ( nuxt . options ) ; options . publicPath = publicPath ; } if ( ! options . swTemplate ) { options . swTemplate = resolve ( PKG_DIR , `templates/workbox/sw${ options . enabled ? '' : '.unregister' }.js` ) ; } if ( ! options . swDest ) { options . swDest = resolve ( nuxt . options . srcDir , nuxt . options . dir . static || 'static' , 'sw.js' ) ; } options . swURL = joinUrl ( options . routerBase , options . swURL || 'sw.js' ) ; if ( ! options . swScope ) { options . swScope = options . routerBase ; } if ( ! options . assetsURLPattern ) { options . assetsURLPattern = options . publicPath ; } if ( options . cacheAssets ) { options . runtimeCaching . push ( { urlPattern : options . assetsURLPattern , handler : nuxt . options . dev ? 'NetworkFirst' : 'CacheFirst' } ) ; } if ( ! options . pagesURLPattern ) { options . pagesURLPattern = options . routerBase ; } if ( options . offline && ! options . offlinePage ) { options . runtimeCaching . push ( { urlPattern : options . pagesURLPattern , handler : options . offlineStrategy } ) ; } if ( ! options . cacheOptions . revision ) { options . cacheOptions . revision = randomString ( 12 ) ; } const normalizePreCaching = ( arr ) => [ ] . concat ( arr ) . map ( url => ( { revision : options . cacheOptions . revision , ... ( typeof url === 'string' ? { url } : url ) } ) ) ; if ( pwa . manifest && pwa . manifest . start_url ) { options . preCaching . unshift ( ... normalizePreCaching ( pwa . manifest . start_url ) ) ; } if ( options . offlineAssets . length ) { options . preCaching . unshift ( ... normalizePreCaching ( options . offlineAssets ) ) ; } if ( options . offlinePage ) { options . preCaching . unshift ( ... ( normalizePreCaching ( options . offlinePage ) ) ) ; } if ( options . cacheOptions . cacheId === undefined ) { options . cacheOptions . cacheId = ( process . env . npm_package_name || 'nuxt' ) + ( nuxt . options . dev ? '-dev' : '-prod' ) ; } options . preCaching = normalizePreCaching ( options . preCaching ) ; const pluginModules = { BackgroundSync : 'backgroundSync.BackgroundSyncPlugin' , BroadcastUpdate : 'broadcastUpdate.BroadcastUpdatePlugin' , CacheableResponse : 'cacheableResponse.CacheableResponsePlugin' , Expiration : 'expiration.ExpirationPlugin' , RangeRequests : 'rangeRequests.RangeRequestsPlugin' } ; options . runtimeCaching = options . runtimeCaching . map ( ( entry ) => { return { ... entry , handler : startCase ( entry . handler ) || 'NetworkFirst' , method : entry . method || 'GET' , strategyPlugins : ( entry . strategyPlugins || [ ] ) . map ( ( plugin ) => { const use = pluginModules [ plugin . use ] ; if ( ! use ) { console . warn ( `Invalid strategy plugin ${ plugin . use }` ) ; return false ; } return { use , config : Array . isArray ( plugin . config ) ? plugin . config : [ plugin . config ] } ; } ) . filter ( Boolean ) } ; } ) ; if ( ! options . workboxURL ) { options . workboxURL = `https://cdn.jsdelivr.net/npm/workbox-cdn@${ options . workboxVersion }/workbox/workbox-sw.js` ; } if ( options . config . debug === undefined ) { options . config . debug = options . dev || nuxt . options . dev ; } return options ; }