runtime-caching-converter.ts
6.11 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {oneLine as ol} from 'common-tags';
import {errors} from './errors';
import {ModuleRegistry} from './module-registry';
import {RuntimeCaching} from '../types';
import {stringifyWithoutComments} from './stringify-without-comments';
/**
* Given a set of options that configures runtime caching behavior, convert it
* to the equivalent Workbox method calls.
*
* @param {ModuleRegistry} moduleRegistry
* @param {Object} options See
* https://developers.google.com/web/tools/workbox/modules/workbox-build#generateSW-runtimeCaching
* @return {string} A JSON string representing the equivalent options.
*
* @private
*/
function getOptionsString(
moduleRegistry: ModuleRegistry,
options: RuntimeCaching['options'] = {},
) {
const plugins: Array<string> = [];
const handlerOptions: {[key in keyof typeof options]: any} = {};
for (const optionName of Object.keys(options) as Array<
keyof typeof options
>) {
if (options[optionName] === undefined) {
continue;
}
switch (optionName) {
// Using a library here because JSON.stringify won't handle functions.
case 'plugins': {
plugins.push(...options.plugins!.map(stringifyWithoutComments));
break;
}
// These are the option properties that we want to pull out, so that
// they're passed to the handler constructor.
case 'cacheName':
case 'networkTimeoutSeconds':
case 'fetchOptions':
case 'matchOptions': {
handlerOptions[optionName] = options[optionName];
break;
}
// The following cases are all shorthands for creating a plugin with a
// given configuration.
case 'backgroundSync': {
const name = options.backgroundSync!.name;
const plugin = moduleRegistry.use(
'workbox-background-sync',
'BackgroundSyncPlugin',
);
let pluginCode = `new ${plugin}(${JSON.stringify(name)}`;
if (options.backgroundSync!.options) {
pluginCode += `, ${stringifyWithoutComments(
options.backgroundSync!.options,
)}`;
}
pluginCode += `)`;
plugins.push(pluginCode);
break;
}
case 'broadcastUpdate': {
const channelName = options.broadcastUpdate!.channelName;
const opts = Object.assign(
{channelName},
options.broadcastUpdate!.options,
);
const plugin = moduleRegistry.use(
'workbox-broadcast-update',
'BroadcastUpdatePlugin',
);
plugins.push(`new ${plugin}(${stringifyWithoutComments(opts)})`);
break;
}
case 'cacheableResponse': {
const plugin = moduleRegistry.use(
'workbox-cacheable-response',
'CacheableResponsePlugin',
);
plugins.push(
`new ${plugin}(${stringifyWithoutComments(
options.cacheableResponse!,
)})`,
);
break;
}
case 'expiration': {
const plugin = moduleRegistry.use(
'workbox-expiration',
'ExpirationPlugin',
);
plugins.push(
`new ${plugin}(${stringifyWithoutComments(options.expiration!)})`,
);
break;
}
case 'precacheFallback': {
const plugin = moduleRegistry.use(
'workbox-precaching',
'PrecacheFallbackPlugin',
);
plugins.push(
`new ${plugin}(${stringifyWithoutComments(
options.precacheFallback!,
)})`,
);
break;
}
case 'rangeRequests': {
const plugin = moduleRegistry.use(
'workbox-range-requests',
'RangeRequestsPlugin',
);
// There are no configuration options for the constructor.
plugins.push(`new ${plugin}()`);
break;
}
default: {
throw new Error(
// In the default case optionName is typed as 'never'.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`${errors['bad-runtime-caching-config']} ${optionName}`,
);
}
}
}
if (Object.keys(handlerOptions).length > 0 || plugins.length > 0) {
const optionsString = JSON.stringify(handlerOptions).slice(1, -1);
return ol`{
${optionsString ? optionsString + ',' : ''}
plugins: [${plugins.join(', ')}]
}`;
} else {
return '';
}
}
export function runtimeCachingConverter(
moduleRegistry: ModuleRegistry,
runtimeCaching: Array<RuntimeCaching>,
): Array<string> {
return runtimeCaching
.map((entry) => {
const method = entry.method || 'GET';
if (!entry.urlPattern) {
throw new Error(errors['urlPattern-is-required']);
}
if (!entry.handler) {
throw new Error(errors['handler-is-required']);
}
if (
entry.options &&
entry.options.networkTimeoutSeconds &&
entry.handler !== 'NetworkFirst'
) {
throw new Error(errors['invalid-network-timeout-seconds']);
}
// urlPattern might be a string, a RegExp object, or a function.
// If it's a string, it needs to be quoted.
const matcher =
typeof entry.urlPattern === 'string'
? JSON.stringify(entry.urlPattern)
: entry.urlPattern;
const registerRoute = moduleRegistry.use(
'workbox-routing',
'registerRoute',
);
if (typeof entry.handler === 'string') {
const optionsString = getOptionsString(moduleRegistry, entry.options);
const handler = moduleRegistry.use('workbox-strategies', entry.handler);
const strategyString = `new ${handler}(${optionsString})`;
return `${registerRoute}(${matcher.toString()}, ${strategyString}, '${method}');\n`;
} else if (typeof entry.handler === 'function') {
return `${registerRoute}(${matcher.toString()}, ${entry.handler.toString()}, '${method}');\n`;
}
// '' will be filtered out.
return '';
})
.filter((entry) => Boolean(entry));
}