-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathoptions.js
98 lines (80 loc) · 2.54 KB
/
options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const path = require('path')
const defaults = require('./defaults')
const { joinUrl, getRouteParams, startCase } = require('@nuxtjs/pwa-utils')
const HMRRegex = '(?!.*(__webpack_hmr|hot-update))'
function getOptions (moduleOptions) {
const options = Object.assign({}, defaults, moduleOptions, this.options.workbox)
// routerBase
if (!options.routerBase) {
options.routerBase = this.options.router.base
}
// publicPath
if (!options.publicPath) {
const { publicPath } = getRouteParams(this.options)
options.publicPath = publicPath
}
// swTemplate
if (!options.swTemplate) {
const disabled = this.options.dev && !options.dev
options.swTemplate = path.resolve(__dirname, `../templates/sw${disabled ? '.disable' : ''}.js`)
}
// swDest
if (!options.swDest) {
options.swDest = path.resolve(this.options.srcDir, this.options.dir.static || 'static', 'sw.js')
}
// swURL
if (!options.swURL) {
options.swURL = joinUrl(options.routerBase, 'sw.js')
}
// swScope
if (!options.swScope) {
options.swScope = options.routerBase
}
// Cache all _nuxt resources at runtime
if (!options.assetsURLPattern) {
options.assetsURLPattern = joinUrl(options.publicPath, HMRRegex)
}
if (options.cacheAssets) {
options.runtimeCaching.push({
urlPattern: options.assetsURLPattern,
handler: 'CacheFirst'
})
}
// Optionally cache other routes for offline
if (!options.pagesURLPattern) {
options.pagesURLPattern = joinUrl(options.routerBase, HMRRegex)
}
if (options.offline && !options.offlinePage) {
options.runtimeCaching.push({
urlPattern: options.pagesURLPattern,
handler: 'NetworkFirst'
})
}
// Add offlineAssets to precaching
if (options.offlineAssets.length) {
options.preCaching.unshift(...options.offlineAssets)
}
// Add offlinePage to precaching
if (options.offlinePage) {
options.preCaching.unshift(options.offlinePage)
}
// Normalize runtimeCaching
options.runtimeCaching = options.runtimeCaching.map(entry => ({
...entry,
handler: startCase(entry.handler) || 'NetworkFirst',
method: entry.method || 'GET'
}))
// Workbox URL
if (!options.workboxURL) {
options.workboxURL = `https://cdn.jsdelivr.net/npm/workbox-cdn@${options.workboxVersion}/workbox/workbox-sw.js`
}
// Workbox Config
if (!options.config.debug) {
// Debug field is by default set to true for localhost domain which is not always ideal
options.config.debug = options.dev || this.options.dev
}
return options
}
module.exports = {
getOptions
}