diff --git a/README.md b/README.md index 9e15deda..2d86abce 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,29 @@ workbox: { **importScripts** (Array) - Additional scripts to be imported in service worker script. (Relative to `/`. Can be placed in `assets/` directory) -For list of all available options see [this table](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#abstract-types) +For list of all available options see [here](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#abstract-types) + +### Adding custom runtimeCaching items (For CDN) + +By default resources in dist (`/_nuxt/`) will be cached with CacheFirst and other requests to same domain will be cached with NetworkFirst strategy. Also all JS webpack emitted resources will be precached. + +If you have a custom CDN and need to cache requests for it, simply use `runtimeCaching`: + +nuxt.config.js +```js +workbox: { + runtimeCaching: [ + { + // Should be a regex string. Compiles into new RegExp('https://google.com/.*') + urlPattern: 'https://my-cdn.com/.*', + // Defaults to `networkFirst` if omitted + handler: 'cacheFirst', + // Defaults to `GET` if omitted + method: 'GET' + } + ] +} +``` ## Icon This module automatically generates app icons and favicon with different sizes using [jimp](https://github.com/oliver-moran/jimp). diff --git a/packages/workbox/index.js b/packages/workbox/index.js index 6f889676..4ba65b7b 100755 --- a/packages/workbox/index.js +++ b/packages/workbox/index.js @@ -58,19 +58,20 @@ function getOptions (moduleOptions) { modifyUrlPrefix: { '': fixUrl(publicPath) }, - runtimeCaching: [ + _runtimeCaching: [ // Cache all _nuxt resources at runtime // They are hashed by webpack so are safe to loaded by cacheFirst handler { - urlPattern: escapeStringRegexp(fixUrl(publicPath + '/')) + '.*', + urlPattern: fixUrl(publicPath + '/.*'), handler: 'cacheFirst' }, // Cache other routes if offline { - urlPattern: escapeStringRegexp(fixUrl(routerBase + '/')) + '.*', + urlPattern: fixUrl(routerBase + '/.*'), handler: 'networkFirst' } - ] + ], + runtimeCaching: [] } const options = defaultsDeep({}, this.options.workbox, moduleOptions, defaults) @@ -89,7 +90,7 @@ function addTemplates (options) { fileName: 'sw.template.js', options: { importScripts: [options.wbDst].concat(options.importScripts || []), - runtimeCaching: options.runtimeCaching.map(i => (Object.assign({}, i, { + runtimeCaching: [].concat(options._runtimeCaching, options.runtimeCaching).map(i => (Object.assign({}, i, { urlPattern: i.urlPattern, handler: i.handler || 'networkFirst', method: i.method || 'GET' diff --git a/packages/workbox/package.json b/packages/workbox/package.json index 6c32dbc9..c6d37119 100755 --- a/packages/workbox/package.json +++ b/packages/workbox/package.json @@ -8,7 +8,6 @@ "access": "public" }, "dependencies": { - "escape-string-regexp": "^1.0.5", "workbox-build": "^2.1.1" } }