@@ -2,6 +2,7 @@ const path = require('path')
2
2
const swBuild = require ( 'workbox-build' )
3
3
const { readFileSync, writeFileSync } = require ( 'fs' )
4
4
const hashSum = require ( 'hash-sum' )
5
+ const escapeStringRegexp = require ( 'escape-string-regexp' )
5
6
const debug = require ( 'debug' ) ( 'nuxt:pwa' )
6
7
7
8
const fixUrl = url => url . replace ( / \/ \/ / g, '/' ) . replace ( ':/' , '://' )
@@ -56,16 +57,16 @@ function getOptions (moduleOptions) {
56
57
'' : fixUrl ( publicPath )
57
58
} ,
58
59
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
65
61
// They are hashed by webpack so are safe to loaded by cacheFirst handler
66
62
{
67
- urlPattern : fixUrl ( publicPath + '/**' ) ,
63
+ urlPattern : escapeStringRegexp ( fixUrl ( publicPath + '/' ) ) + '.*' ,
68
64
handler : 'cacheFirst'
65
+ } ,
66
+ // Cache other routes if offline
67
+ {
68
+ urlPattern : escapeStringRegexp ( fixUrl ( routerBase + '/' ) ) + '.*' ,
69
+ handler : 'networkFirst'
69
70
}
70
71
]
71
72
} , moduleOptions , this . options . workbox )
@@ -84,7 +85,11 @@ function addTemplates (options) {
84
85
fileName : 'sw.template.js' ,
85
86
options : {
86
87
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
+ } ) ) ) ,
88
93
wbOptions : {
89
94
cacheId : options . cacheId ,
90
95
clientsClaim : options . clientsClaim ,
0 commit comments