Permalink
Browse files

feat(runtimeCaching): Support runtimeCaching

  • Loading branch information...
pi0 committed Nov 17, 2017
1 parent 7b2c1af commit fdcda0f420b01f637e9ddd8b7ad127c8e5cf2e86
Showing with 29 additions and 7 deletions.
  1. +23 −1 README.md
  2. +6 −5 packages/workbox/index.js
  3. +0 −1 packages/workbox/package.json
@@ -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).
@@ -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'
@@ -8,7 +8,6 @@
"access": "public"
},
"dependencies": {
"escape-string-regexp": "^1.0.5",
"workbox-build": "^2.1.1"
}
}

0 comments on commit fdcda0f

Please sign in to comment.