Skip to content

Commit

Permalink
fix(workbox): use regexp for runtimeCaching. fixes #14.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Nov 17, 2017
1 parent 6bd0507 commit f384103
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
21 changes: 13 additions & 8 deletions packages/workbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const path = require('path')
const swBuild = require('workbox-build')
const { readFileSync, writeFileSync } = require('fs')
const hashSum = require('hash-sum')
const escapeStringRegexp = require('escape-string-regexp')
const debug = require('debug')('nuxt:pwa')

const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://')
Expand Down Expand Up @@ -56,16 +57,16 @@ function getOptions (moduleOptions) {
'': fixUrl(publicPath)
},
runtimeCaching: [
// Cache routes if offline
{
urlPattern: fixUrl(routerBase + '/**'),
handler: 'networkFirst'
},
// Cache other _nuxt resources runtime
// Cache all _nuxt resources at runtime
// They are hashed by webpack so are safe to loaded by cacheFirst handler
{
urlPattern: fixUrl(publicPath + '/**'),
urlPattern: escapeStringRegexp(fixUrl(publicPath + '/')) + '.*',
handler: 'cacheFirst'
},
// Cache other routes if offline
{
urlPattern: escapeStringRegexp(fixUrl(routerBase + '/')) + '.*',
handler: 'networkFirst'
}
]
}, moduleOptions, this.options.workbox)
Expand All @@ -84,7 +85,11 @@ function addTemplates (options) {
fileName: 'sw.template.js',
options: {
importScripts: [options.wbDst].concat(options.importScripts || []),
runtimeCaching: options.runtimeCaching,
runtimeCaching: options.runtimeCaching.map(i => (Object.assign({}, i, {
urlPattern: i.urlPattern,
handler: i.handler || 'networkFirst',
method: i.method || 'GET'
}))),
wbOptions: {
cacheId: options.cacheId,
clientsClaim: options.clientsClaim,
Expand Down
1 change: 1 addition & 0 deletions packages/workbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"access": "public"
},
"dependencies": {
"escape-string-regexp": "^1.0.5",
"workbox-build": "^2.1.1"
}
}
2 changes: 1 addition & 1 deletion packages/workbox/templates/sw.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ const workboxSW = new self.WorkboxSW(<%= JSON.stringify(options.wbOptions, null,
workboxSW.precache([])

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

0 comments on commit f384103

Please sign in to comment.