/
options.js
98 lines (81 loc) · 2.65 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('../utils')
function getOptions (workboxOptions) {
const options = { ...defaults, ...workboxOptions }
// 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
options.swURL = joinUrl(options.routerBase, options.swURL || 'sw.js')
// swScope
if (!options.swScope) {
options.swScope = options.routerBase
}
// Cache all _nuxt resources at runtime
if (!options.assetsURLPattern) {
options.assetsURLPattern = options.publicPath
}
if (options.cacheAssets) {
options.runtimeCaching.push({
urlPattern: options.assetsURLPattern,
handler: this.options.dev ? 'NetworkFirst' : 'CacheFirst'
})
}
// Optionally cache other routes for offline
if (!options.pagesURLPattern) {
options.pagesURLPattern = options.routerBase
}
if (options.offline && !options.offlinePage) {
options.runtimeCaching.push({
urlPattern: options.pagesURLPattern,
handler: options.offlineStrategy
})
}
// 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)
}
// Default cacheId
if (options.cacheOptions.cacheId === undefined) {
options.cacheOptions.cacheId = (process.env.npm_package_name || 'nuxt') + (this.options.dev ? '-dev' : '-prod')
}
// 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 === undefined) {
// 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
}