1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const ol = require('common-tags').oneLine;
|
10 |
|
11 | const errors = require('./errors');
|
12 | const stringifyWithoutComments = require('./stringify-without-comments');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function getOptionsString(options = {}) {
|
25 | let plugins = [];
|
26 | if (options.plugins) {
|
27 |
|
28 | plugins = options.plugins.map(stringifyWithoutComments);
|
29 | delete options.plugins;
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | const handlerOptionKeys = [
|
36 | 'cacheName',
|
37 | 'networkTimeoutSeconds',
|
38 | 'fetchOptions',
|
39 | 'matchOptions',
|
40 | ];
|
41 | const handlerOptions = {};
|
42 | for (const key of handlerOptionKeys) {
|
43 | if (key in options) {
|
44 | handlerOptions[key] = options[key];
|
45 | delete options[key];
|
46 | }
|
47 | }
|
48 |
|
49 | const pluginsMapping = {
|
50 | backgroundSync: 'workbox.backgroundSync.Plugin',
|
51 | broadcastUpdate: 'workbox.broadcastUpdate.Plugin',
|
52 | expiration: 'workbox.expiration.Plugin',
|
53 | cacheableResponse: 'workbox.cacheableResponse.Plugin',
|
54 | };
|
55 |
|
56 | for (const [pluginName, pluginConfig] of Object.entries(options)) {
|
57 |
|
58 | if (Object.keys(pluginConfig).length === 0) {
|
59 | continue;
|
60 | }
|
61 |
|
62 | const pluginString = pluginsMapping[pluginName];
|
63 | if (!pluginString) {
|
64 | throw new Error(`${errors['bad-runtime-caching-config']} ${pluginName}`);
|
65 | }
|
66 |
|
67 | let pluginCode;
|
68 | switch (pluginName) {
|
69 |
|
70 |
|
71 | case 'backgroundSync': {
|
72 | const name = pluginConfig.name;
|
73 | pluginCode = `new ${pluginString}(${JSON.stringify(name)}`;
|
74 | if ('options' in pluginConfig) {
|
75 | pluginCode += `, ${stringifyWithoutComments(pluginConfig.options)}`;
|
76 | }
|
77 | pluginCode += `)`;
|
78 |
|
79 | break;
|
80 | }
|
81 |
|
82 | case 'broadcastUpdate': {
|
83 | const channelName = pluginConfig.channelName;
|
84 | pluginCode = `new ${pluginString}(${JSON.stringify(channelName)}`;
|
85 | if ('options' in pluginConfig) {
|
86 | pluginCode += `, ${stringifyWithoutComments(pluginConfig.options)}`;
|
87 | }
|
88 | pluginCode += `)`;
|
89 |
|
90 | break;
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 | default: {
|
96 | pluginCode = `new ${pluginString}(${stringifyWithoutComments(
|
97 | pluginConfig
|
98 | )})`;
|
99 | }
|
100 | }
|
101 |
|
102 | plugins.push(pluginCode);
|
103 | }
|
104 |
|
105 | if (Object.keys(handlerOptions).length > 0 || plugins.length > 0) {
|
106 | const optionsString = JSON.stringify(handlerOptions).slice(1, -1);
|
107 | return ol`{
|
108 | ${optionsString ? optionsString + ',' : ''}
|
109 | plugins: [${plugins.join(', ')}]
|
110 | }`;
|
111 | } else {
|
112 | return '';
|
113 | }
|
114 | }
|
115 |
|
116 | module.exports = (runtimeCaching = []) => {
|
117 | return runtimeCaching.map((entry) => {
|
118 | const method = entry.method || 'GET';
|
119 |
|
120 | if (!entry.urlPattern) {
|
121 | throw new Error(errors['urlPattern-is-required']);
|
122 | }
|
123 |
|
124 | if (!entry.handler) {
|
125 | throw new Error(errors['handler-is-required']);
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 | if (entry.options && entry.options.networkTimeoutSeconds &&
|
131 | entry.handler !== 'NetworkFirst') {
|
132 | throw new Error(errors['invalid-network-timeout-seconds']);
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 | const matcher = typeof entry.urlPattern === 'string' ?
|
138 | JSON.stringify(entry.urlPattern) :
|
139 | entry.urlPattern;
|
140 |
|
141 | if (typeof entry.handler === 'string') {
|
142 | const optionsString = getOptionsString(entry.options || {});
|
143 | const strategyString =
|
144 | `new workbox.strategies.${entry.handler}(${optionsString})`;
|
145 |
|
146 | return `workbox.routing.registerRoute(` +
|
147 | `${matcher}, ${strategyString}, '${method}');\n`;
|
148 | } else if (typeof entry.handler === 'function') {
|
149 | return `workbox.routing.registerRoute(` +
|
150 | `${matcher}, ${entry.handler}, '${method}');\n`;
|
151 | }
|
152 | }).filter((entry) => Boolean(entry));
|
153 | };
|