@@ -12,19 +12,16 @@ const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
12
12
// =============================================
13
13
14
14
module . exports = function nuxtWorkbox ( moduleOptions ) {
15
- const options = Object . assign ( { } , moduleOptions , this . options . workbox )
16
- const ctx = { options }
17
-
18
15
if ( this . options . dev ) {
19
16
return
20
17
}
21
18
22
19
const hook = builder => {
23
20
debug ( 'Adding workbox' )
24
- getRouterBase . call ( this , ctx )
25
- workboxInject . call ( this , ctx )
26
- emitAssets . call ( this , ctx )
27
- addTemplates . call ( this , ctx )
21
+ const options = getOptions . call ( this , moduleOptions )
22
+ workboxInject . call ( this , options )
23
+ emitAssets . call ( this , options )
24
+ addTemplates . call ( this , options )
28
25
}
29
26
30
27
this . nuxt . hook ? this . nuxt . hook ( 'build:before' , hook ) : this . nuxt . plugin ( 'build' , hook )
@@ -34,7 +31,8 @@ module.exports = function nuxtWorkbox (moduleOptions) {
34
31
// getRouterBase
35
32
// =============================================
36
33
37
- function getRouterBase ( ctx ) {
34
+ function getOptions ( moduleOptions ) {
35
+ // Router Base
38
36
const routerBase = this . options . router . base
39
37
let publicPath = fixUrl ( `${ routerBase } /${ this . options . build . publicPath } ` )
40
38
if ( isUrl ( this . options . build . publicPath ) ) { // CDN
@@ -44,34 +42,66 @@ function getRouterBase (ctx) {
44
42
}
45
43
}
46
44
47
- ctx . routerBase = routerBase
48
- ctx . publicPath = publicPath
45
+ const options = Object . assign ( {
46
+ routerBase,
47
+ publicPath,
48
+ swSrc : path . resolve ( this . options . buildDir , 'sw.template.js' ) ,
49
+ swDest : path . resolve ( this . options . srcDir , 'static' , 'sw.js' ) ,
50
+ directoryIndex : '/' ,
51
+ cacheId : process . env . npm_package_name + '_' + process . env . npm_package_version ,
52
+ clientsClaim : true ,
53
+ globPatterns : [ '**/*.{js,css}' ] ,
54
+ globDirectory : path . resolve ( this . options . buildDir , 'dist' ) ,
55
+ modifyUrlPrefix : {
56
+ '' : fixUrl ( publicPath )
57
+ } ,
58
+ runtimeCaching : [
59
+ // Cache routes if offline
60
+ {
61
+ urlPattern : fixUrl ( routerBase + '/**' ) ,
62
+ handler : 'networkFirst'
63
+ } ,
64
+ // Cache other _nuxt resources runtime
65
+ // They are hashed by webpack so are safe to loaded by cacheFirst handler
66
+ {
67
+ urlPattern : fixUrl ( publicPath + '/**' ) ,
68
+ handler : 'cacheFirst'
69
+ }
70
+ ]
71
+ } , moduleOptions , this . options . workbox )
72
+
73
+ return options
49
74
}
50
75
51
76
// =============================================
52
77
// addTemplates
53
78
// =============================================
54
79
55
- function addTemplates ( ctx ) {
80
+ function addTemplates ( options ) {
56
81
// Add sw.template.js
57
82
this . addTemplate ( {
58
83
src : path . resolve ( __dirname , 'templates/sw.template.js' ) ,
59
84
fileName : 'sw.template.js' ,
60
85
options : {
61
- importPath : ctx . wbDst ,
62
- wb : ctx . workboxOptions
86
+ importScripts : [ options . wbDst ] . concat ( options . importScripts || [ ] ) ,
87
+ runtimeCaching : options . runtimeCaching ,
88
+ wbOptions : {
89
+ cacheId : options . cacheId ,
90
+ clientsClaim : options . clientsClaim ,
91
+ directoryIndex : options . directoryIndex
92
+ }
63
93
}
64
94
} )
65
95
66
96
// Add sw.plugin.js
67
- const swURL = `${ ctx . routerBase } /${ ctx . options . swURL || 'sw.js' } `
97
+ const swURL = `${ options . routerBase } /${ options . swURL || 'sw.js' } `
68
98
this . addPlugin ( {
69
99
src : path . resolve ( __dirname , 'templates/sw.plugin.js' ) ,
70
100
ssr : false ,
71
101
fileName : 'sw.plugin.js' ,
72
102
options : {
73
103
swURL : fixUrl ( swURL ) ,
74
- swScope : fixUrl ( `${ ctx . routerBase } /` )
104
+ swScope : fixUrl ( `${ options . routerBase } /` )
75
105
}
76
106
} )
77
107
}
@@ -80,7 +110,7 @@ function addTemplates (ctx) {
80
110
// emitAssets
81
111
// =============================================
82
112
83
- function emitAssets ( ctx ) {
113
+ function emitAssets ( options ) {
84
114
const assets = [ ]
85
115
const emitAsset = ( path , name , ext = 'js' ) => {
86
116
const source = readFileSync ( path )
@@ -107,46 +137,19 @@ function emitAssets (ctx) {
107
137
108
138
// workbox.js
109
139
let wbPath = require . resolve ( 'workbox-sw' )
110
- if ( ctx . options . dev ) {
140
+ if ( options . dev ) {
111
141
wbPath = wbPath . replace ( / p r o d / g, 'dev' )
112
142
}
113
- ctx . wbDst = fixUrl ( ctx . publicPath + '/' + emitAsset ( wbPath , 'workbox' + ( ctx . options . dev ? '.dev' : '' ) ) )
143
+ options . wbDst = fixUrl ( options . publicPath + '/' + emitAsset ( wbPath , 'workbox' + ( options . dev ? '.dev' : '' ) ) )
114
144
}
115
145
116
146
// =============================================
117
147
// workboxInject
118
148
// =============================================
119
149
120
- function workboxInject ( ctx ) {
121
- // https://workboxjs.org/reference-docs/latest/module-workbox-build.html#.generateSW
122
- ctx . workboxOptions = Object . assign ( {
123
- swSrc : path . resolve ( this . options . buildDir , 'sw.template.js' ) ,
124
- swDest : path . resolve ( this . options . srcDir , 'static' , 'sw.js' ) ,
125
- directoryIndex : '/' ,
126
- cacheId : process . env . npm_package_name + '_' + process . env . npm_package_version ,
127
- clientsClaim : true ,
128
- globPatterns : [ '**/*.{js,css}' ] ,
129
- globDirectory : path . resolve ( this . options . buildDir , 'dist' ) ,
130
- modifyUrlPrefix : {
131
- '' : fixUrl ( ctx . publicPath )
132
- } ,
133
- runtimeCaching : [
134
- // Cache routes if offline
135
- {
136
- urlPattern : fixUrl ( ctx . routerBase + '/**' ) ,
137
- handler : 'networkFirst'
138
- } ,
139
- // Cache other _nuxt resources runtime
140
- // They are hashed by webpack so are safe to loaded by cacheFirst handler
141
- {
142
- urlPattern : fixUrl ( ctx . publicPath + '/**' ) ,
143
- handler : 'cacheFirst'
144
- }
145
- ]
146
- } , ctx . options )
147
-
150
+ function workboxInject ( options ) {
148
151
const hook = ( ) => {
149
- const opts = Object . assign ( { } , ctx . workboxOptions )
152
+ const opts = Object . assign ( { } , options )
150
153
delete opts . runtimeCaching
151
154
return swBuild . injectManifest ( opts )
152
155
}
0 commit comments