Skip to content

Commit fdcda0f

Browse files
author
Pooya Parsa
committedNov 17, 2017
feat(runtimeCaching): Support runtimeCaching
1 parent 7b2c1af commit fdcda0f

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed
 

‎README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,29 @@ workbox: {
104104

105105
**importScripts** (Array) - Additional scripts to be imported in service worker script. (Relative to `/`. Can be placed in `assets/` directory)
106106

107-
For list of all available options see [this table](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#abstract-types)
107+
For list of all available options see [here](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#abstract-types)
108+
109+
### Adding custom runtimeCaching items (For CDN)
110+
111+
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.
112+
113+
If you have a custom CDN and need to cache requests for it, simply use `runtimeCaching`:
114+
115+
nuxt.config.js
116+
```js
117+
workbox: {
118+
runtimeCaching: [
119+
{
120+
// Should be a regex string. Compiles into new RegExp('https://google.com/.*')
121+
urlPattern: 'https://my-cdn.com/.*',
122+
// Defaults to `networkFirst` if omitted
123+
handler: 'cacheFirst',
124+
// Defaults to `GET` if omitted
125+
method: 'GET'
126+
}
127+
]
128+
}
129+
```
108130

109131
## Icon
110132
This module automatically generates app icons and favicon with different sizes using [jimp](https://github.com/oliver-moran/jimp).

‎packages/workbox/index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,20 @@ function getOptions (moduleOptions) {
5858
modifyUrlPrefix: {
5959
'': fixUrl(publicPath)
6060
},
61-
runtimeCaching: [
61+
_runtimeCaching: [
6262
// Cache all _nuxt resources at runtime
6363
// They are hashed by webpack so are safe to loaded by cacheFirst handler
6464
{
65-
urlPattern: escapeStringRegexp(fixUrl(publicPath + '/')) + '.*',
65+
urlPattern: fixUrl(publicPath + '/.*'),
6666
handler: 'cacheFirst'
6767
},
6868
// Cache other routes if offline
6969
{
70-
urlPattern: escapeStringRegexp(fixUrl(routerBase + '/')) + '.*',
70+
urlPattern: fixUrl(routerBase + '/.*'),
7171
handler: 'networkFirst'
7272
}
73-
]
73+
],
74+
runtimeCaching: []
7475
}
7576

7677
const options = defaultsDeep({}, this.options.workbox, moduleOptions, defaults)
@@ -89,7 +90,7 @@ function addTemplates (options) {
8990
fileName: 'sw.template.js',
9091
options: {
9192
importScripts: [options.wbDst].concat(options.importScripts || []),
92-
runtimeCaching: options.runtimeCaching.map(i => (Object.assign({}, i, {
93+
runtimeCaching: [].concat(options._runtimeCaching, options.runtimeCaching).map(i => (Object.assign({}, i, {
9394
urlPattern: i.urlPattern,
9495
handler: i.handler || 'networkFirst',
9596
method: i.method || 'GET'

‎packages/workbox/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"access": "public"
99
},
1010
"dependencies": {
11-
"escape-string-regexp": "^1.0.5",
1211
"workbox-build": "^2.1.1"
1312
}
1413
}

0 commit comments

Comments
 (0)
Please sign in to comment.