diff --git a/docs/modules/workbox.md b/docs/modules/workbox.md index fe788714..654e9465 100644 --- a/docs/modules/workbox.md +++ b/docs/modules/workbox.md @@ -58,6 +58,10 @@ workbox: { (String) Loads and inserts the contents of the specified file path into the service worker script, below autogenerated calls to `workbox.routing.*`. You may add as many extra calls as you want to this file. +### `workboxExtensions` + +(String) Loads and inserts the contents of the specified file path into the service worker script before any call to `precacheAndRoute`. You may add as many extra calls as you want to this file. + ### `clientsClaim` (Boolean) Start controlling any existing clients as soon as it activates. Enabled by default. @@ -102,6 +106,7 @@ workbox: { (String) Defaults to `routerBase`. + ## Examples ### Adding custom runtimeCaching items (For CDN) @@ -234,3 +239,5 @@ workbox.routing.registerRoute( ``` Thanks to [@CarterLi](https://github.com/CarterLi) for the tip. + +### Enable diff --git a/packages/workbox/lib/module.js b/packages/workbox/lib/module.js index 8d041942..feee8823 100755 --- a/packages/workbox/lib/module.js +++ b/packages/workbox/lib/module.js @@ -27,8 +27,9 @@ module.exports = function nuxtWorkbox (moduleOptions) { fileName: options.swDest, options: { ...options, - routingExtensions: readJSFiles(options.routingExtensions), - cachingExtensions: readJSFiles(options.cachingExtensions) + routingExtensions: readJSFiles.call(this, options.routingExtensions), + cachingExtensions: readJSFiles.call(this, options.cachingExtensions), + workboxExtensions: readJSFiles.call(this, options.workboxExtensions) } }) } diff --git a/packages/workbox/lib/utils.js b/packages/workbox/lib/utils.js index 5e8c0786..1ff442c7 100644 --- a/packages/workbox/lib/utils.js +++ b/packages/workbox/lib/utils.js @@ -2,9 +2,8 @@ const { readFileSync, existsSync } = require('fs') function readJSFiles (files) { return Array.from(files) - .map(Boolean) .map(path => { - path = this.nuxt.resolveAlias(path) + path = this.nuxt.resolver.resolvePath(path) if (path && existsSync(path)) { return readFileSync(path, 'utf8') } else { diff --git a/packages/workbox/templates/sw.js b/packages/workbox/templates/sw.js index 38c9c6c8..f834ec36 100644 --- a/packages/workbox/templates/sw.js +++ b/packages/workbox/templates/sw.js @@ -24,6 +24,11 @@ workbox.skipWaiting() workbox.googleAnalytics.initialize() <% } %> +<% if (options.workboxExtensions) { %> +// -- Start of workboxExtensions -- +<%= options.workboxExtensions %>// -- End of workboxExtensions -- +<% } %> + // -------------------------------------------------- // Precaches // -------------------------------------------------- @@ -46,8 +51,7 @@ workbox.precaching.precacheAndRoute(['<%= options.offlinePage %>']) // -------------------------------------------------- <% if (options.cachingExtensions) { %> // -- Start of cachingExtensions -- -<%= options.cachingExtensions %> -// -- End of cachingExtensions -- +<%= options.cachingExtensions %>// -- End of cachingExtensions -- <% } %> // Register route handlers for runtimeCaching @@ -63,6 +67,5 @@ workbox.routing.registerRoute(new RegExp('/.*'), ({event}) => { <% if (options.routingExtensions) { %> // -- Start of routingExtensions -- -<%= options.routingExtensions %> -// -- End of routingExtensions -- +<%= options.routingExtensions %>// -- End of routingExtensions -- <% } %> diff --git a/test/__snapshots__/pwa.test.js.snap b/test/__snapshots__/pwa.test.js.snap index 19a12eea..80f006cf 100644 --- a/test/__snapshots__/pwa.test.js.snap +++ b/test/__snapshots__/pwa.test.js.snap @@ -71,6 +71,10 @@ workbox.skipWaiting() // Enable offline Google Analytics tracking workbox.googleAnalytics.initialize() +// -- Start of workboxExtensions -- +// Workbox Extension +// -- End of workboxExtensions -- + // -------------------------------------------------- // Precaches // -------------------------------------------------- @@ -85,9 +89,17 @@ workbox.precaching.precacheAndRoute([], { // Runtime Caching // -------------------------------------------------- +// -- Start of cachingExtensions -- +// Caching Extension +// -- End of cachingExtensions -- + // Register route handlers for runtimeCaching workbox.routing.registerRoute(new RegExp('https://google.com/.*'), workbox.strategies.cacheFirst ({}), 'GET') workbox.routing.registerRoute(new RegExp('/_nuxt/(?!.*(__webpack_hmr|hot-update))'), workbox.strategies.cacheFirst ({}), 'GET') workbox.routing.registerRoute(new RegExp('^/(?!.*(__webpack_hmr|hot-update))'), workbox.strategies.networkFirst ({}), 'GET') + +// -- Start of routingExtensions -- +// Routing Extension +// -- End of routingExtensions -- " `; diff --git a/test/fixture/nuxt.config.js b/test/fixture/nuxt.config.js index 493052a3..d95bf86c 100644 --- a/test/fixture/nuxt.config.js +++ b/test/fixture/nuxt.config.js @@ -28,6 +28,15 @@ module.exports = { importScripts: [ 'custom-sw.js' ], + workboxExtensions: [ + '~/sw/workbox' + ], + cachingExtensions: [ + '~/sw/caching' + ], + routingExtensions: [ + '~/sw/routing' + ], runtimeCaching: [ { urlPattern: 'https://google.com/.*', diff --git a/test/fixture/sw/caching.js b/test/fixture/sw/caching.js new file mode 100644 index 00000000..cfd61f07 --- /dev/null +++ b/test/fixture/sw/caching.js @@ -0,0 +1 @@ +// Caching Extension diff --git a/test/fixture/sw/routing.js b/test/fixture/sw/routing.js new file mode 100644 index 00000000..14dd4146 --- /dev/null +++ b/test/fixture/sw/routing.js @@ -0,0 +1 @@ +// Routing Extension diff --git a/test/fixture/sw/workbox.js b/test/fixture/sw/workbox.js new file mode 100644 index 00000000..d00deacf --- /dev/null +++ b/test/fixture/sw/workbox.js @@ -0,0 +1 @@ +// Workbox Extension