Skip to content

Commit

Permalink
feat(runtimeCaching): Support runtimeCaching
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Nov 17, 2017
1 parent 7b2c1af commit fdcda0f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
24 changes: 23 additions & 1 deletion README.md
Expand Up @@ -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).
Expand Down
11 changes: 6 additions & 5 deletions packages/workbox/index.js
Expand Up @@ -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)
Expand All @@ -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'
Expand Down
1 change: 0 additions & 1 deletion packages/workbox/package.json
Expand Up @@ -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.