Skip to content

Commit f384103

Browse files
author
Pooya Parsa
committedNov 17, 2017
fix(workbox): use regexp for runtimeCaching. fixes #14.
1 parent 6bd0507 commit f384103

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed
 

Diff for: ‎packages/workbox/index.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path')
22
const swBuild = require('workbox-build')
33
const { readFileSync, writeFileSync } = require('fs')
44
const hashSum = require('hash-sum')
5+
const escapeStringRegexp = require('escape-string-regexp')
56
const debug = require('debug')('nuxt:pwa')
67

78
const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://')
@@ -56,16 +57,16 @@ function getOptions (moduleOptions) {
5657
'': fixUrl(publicPath)
5758
},
5859
runtimeCaching: [
59-
// Cache routes if offline
60-
{
61-
urlPattern: fixUrl(routerBase + '/**'),
62-
handler: 'networkFirst'
63-
},
64-
// Cache other _nuxt resources runtime
60+
// Cache all _nuxt resources at runtime
6561
// They are hashed by webpack so are safe to loaded by cacheFirst handler
6662
{
67-
urlPattern: fixUrl(publicPath + '/**'),
63+
urlPattern: escapeStringRegexp(fixUrl(publicPath + '/')) + '.*',
6864
handler: 'cacheFirst'
65+
},
66+
// Cache other routes if offline
67+
{
68+
urlPattern: escapeStringRegexp(fixUrl(routerBase + '/')) + '.*',
69+
handler: 'networkFirst'
6970
}
7071
]
7172
}, moduleOptions, this.options.workbox)
@@ -84,7 +85,11 @@ function addTemplates (options) {
8485
fileName: 'sw.template.js',
8586
options: {
8687
importScripts: [options.wbDst].concat(options.importScripts || []),
87-
runtimeCaching: options.runtimeCaching,
88+
runtimeCaching: options.runtimeCaching.map(i => (Object.assign({}, i, {
89+
urlPattern: i.urlPattern,
90+
handler: i.handler || 'networkFirst',
91+
method: i.method || 'GET'
92+
}))),
8893
wbOptions: {
8994
cacheId: options.cacheId,
9095
clientsClaim: options.clientsClaim,

Diff for: ‎packages/workbox/package.json

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

Diff for: ‎packages/workbox/templates/sw.template.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ const workboxSW = new self.WorkboxSW(<%= JSON.stringify(options.wbOptions, null,
55
workboxSW.precache([])
66

77
<% options.runtimeCaching.forEach(r => { %>
8-
workboxSW.router.registerRoute('<%= r.urlPattern %>', workboxSW.strategies.<%= r.handler %>({}), 'GET')
8+
workboxSW.router.registerRoute(new RegExp('<%= r.urlPattern %>'), workboxSW.strategies.<%= r.handler %>({}), '<%= r.method %>')
99
<% }) %>

0 commit comments

Comments
 (0)
Please sign in to comment.