jekyll-pwa-workbox 5.1.4 → 5.1.41
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jekyll-pwa-workbox.rb +1 -1
- data/lib/vendor/workbox-v5.1.4/workbox-background-sync.dev.js +818 -818
- data/lib/vendor/workbox-v5.1.4/workbox-background-sync.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-broadcast-update.dev.js +288 -288
- data/lib/vendor/workbox-v5.1.4/workbox-broadcast-update.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-cacheable-response.dev.js +191 -191
- data/lib/vendor/workbox-v5.1.4/workbox-cacheable-response.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-core.dev.js +1858 -1858
- data/lib/vendor/workbox-v5.1.4/workbox-core.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-expiration.dev.js +649 -649
- data/lib/vendor/workbox-v5.1.4/workbox-expiration.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-navigation-preload.dev.js +102 -102
- data/lib/vendor/workbox-v5.1.4/workbox-navigation-preload.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-offline-ga.dev.js +235 -235
- data/lib/vendor/workbox-v5.1.4/workbox-offline-ga.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-precaching.dev.js +1210 -1210
- data/lib/vendor/workbox-v5.1.4/workbox-precaching.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-range-requests.dev.js +262 -262
- data/lib/vendor/workbox-v5.1.4/workbox-range-requests.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-routing.dev.js +923 -923
- data/lib/vendor/workbox-v5.1.4/workbox-routing.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-strategies.dev.js +923 -923
- data/lib/vendor/workbox-v5.1.4/workbox-strategies.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-streams.dev.js +318 -318
- data/lib/vendor/workbox-v5.1.4/workbox-streams.prod.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-sw.js +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-window.dev.es5.mjs +1125 -1125
- data/lib/vendor/workbox-v5.1.4/workbox-window.dev.mjs +943 -943
- data/lib/vendor/workbox-v5.1.4/workbox-window.dev.umd.js +1136 -1136
- data/lib/vendor/workbox-v5.1.4/workbox-window.prod.es5.mjs +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-window.prod.mjs +2 -2
- data/lib/vendor/workbox-v5.1.4/workbox-window.prod.umd.js +2 -2
- metadata +2 -2
@@ -1,2 +1,2 @@
|
|
1
|
-
this.workbox=this.workbox||{},this.workbox.rangeRequests=function(t,e,n){"use strict";try{self["workbox:range-requests:5.1.4"]&&_()}catch(t){}async function r(t,n){try{if(206===n.status)return n;const r=t.headers.get("range");if(!r)throw new e.WorkboxError("no-range-header");const s=function(t){const n=t.trim().toLowerCase();if(!n.startsWith("bytes="))throw new e.WorkboxError("unit-must-be-bytes",{normalizedRangeHeader:n});if(n.includes(","))throw new e.WorkboxError("single-range-only",{normalizedRangeHeader:n});const r=/(\d*)-(\d*)/.exec(n);if(!r||!r[1]&&!r[2])throw new e.WorkboxError("invalid-range-values",{normalizedRangeHeader:n});return{start:""===r[1]?void 0:Number(r[1]),end:""===r[2]?void 0:Number(r[2])}}(r),a=await n.blob(),o=function(t,n,r){const s=t.size;if(r&&r>s||n&&n<0)throw new e.WorkboxError("range-not-satisfiable",{size:s,end:r,start:n});let a,o;return void 0!==n&&void 0!==r?(a=n,o=r+1):void 0!==n&&void 0===r?(a=n,o=s):void 0!==r&&void 0===n&&(a=s-r,o=s),{start:a,end:o}}(a,s.start,s.end),i=a.slice(o.start,o.end),d=i.size,u=new Response(i,{status:206,statusText:"Partial Content",headers:n.headers});return u.headers.set("Content-Length",String(d)),u.headers.set("Content-Range",`bytes ${o.start}-${o.end-1}/`+a.size),u}catch(t){return new Response("",{status:416,statusText:"Range Not Satisfiable"})}}return t.RangeRequestsPlugin=class{constructor(){this.cachedResponseWillBeUsed=async({request:t,cachedResponse:e})=>e&&t.headers.has("range")?await r(t,e):e}},t.createPartialResponse=r,t}({},workbox.core._private,workbox.core._private);
|
2
|
-
//# sourceMappingURL=workbox-range-requests.prod.js.map
|
1
|
+
this.workbox=this.workbox||{},this.workbox.rangeRequests=function(t,e,n){"use strict";try{self["workbox:range-requests:5.1.4"]&&_()}catch(t){}async function r(t,n){try{if(206===n.status)return n;const r=t.headers.get("range");if(!r)throw new e.WorkboxError("no-range-header");const s=function(t){const n=t.trim().toLowerCase();if(!n.startsWith("bytes="))throw new e.WorkboxError("unit-must-be-bytes",{normalizedRangeHeader:n});if(n.includes(","))throw new e.WorkboxError("single-range-only",{normalizedRangeHeader:n});const r=/(\d*)-(\d*)/.exec(n);if(!r||!r[1]&&!r[2])throw new e.WorkboxError("invalid-range-values",{normalizedRangeHeader:n});return{start:""===r[1]?void 0:Number(r[1]),end:""===r[2]?void 0:Number(r[2])}}(r),a=await n.blob(),o=function(t,n,r){const s=t.size;if(r&&r>s||n&&n<0)throw new e.WorkboxError("range-not-satisfiable",{size:s,end:r,start:n});let a,o;return void 0!==n&&void 0!==r?(a=n,o=r+1):void 0!==n&&void 0===r?(a=n,o=s):void 0!==r&&void 0===n&&(a=s-r,o=s),{start:a,end:o}}(a,s.start,s.end),i=a.slice(o.start,o.end),d=i.size,u=new Response(i,{status:206,statusText:"Partial Content",headers:n.headers});return u.headers.set("Content-Length",String(d)),u.headers.set("Content-Range",`bytes ${o.start}-${o.end-1}/`+a.size),u}catch(t){return new Response("",{status:416,statusText:"Range Not Satisfiable"})}}return t.RangeRequestsPlugin=class{constructor(){this.cachedResponseWillBeUsed=async({request:t,cachedResponse:e})=>e&&t.headers.has("range")?await r(t,e):e}},t.createPartialResponse=r,t}({},workbox.core._private,workbox.core._private);
|
2
|
+
//# sourceMappingURL=workbox-range-requests.prod.js.map
|
@@ -1,923 +1,923 @@
|
|
1
|
-
this.workbox = this.workbox || {};
|
2
|
-
this.workbox.routing = (function (exports, assert_js, logger_js, WorkboxError_js, getFriendlyURL_js) {
|
3
|
-
'use strict';
|
4
|
-
|
5
|
-
try {
|
6
|
-
self['workbox:routing:5.1.4'] && _();
|
7
|
-
} catch (e) {}
|
8
|
-
|
9
|
-
/*
|
10
|
-
Copyright 2018 Google LLC
|
11
|
-
|
12
|
-
Use of this source code is governed by an MIT-style
|
13
|
-
license that can be found in the LICENSE file or at
|
14
|
-
https://opensource.org/licenses/MIT.
|
15
|
-
*/
|
16
|
-
/**
|
17
|
-
* The default HTTP method, 'GET', used when there's no specific method
|
18
|
-
* configured for a route.
|
19
|
-
*
|
20
|
-
* @type {string}
|
21
|
-
*
|
22
|
-
* @private
|
23
|
-
*/
|
24
|
-
|
25
|
-
const defaultMethod = 'GET';
|
26
|
-
/**
|
27
|
-
* The list of valid HTTP methods associated with requests that could be routed.
|
28
|
-
*
|
29
|
-
* @type {Array<string>}
|
30
|
-
*
|
31
|
-
* @private
|
32
|
-
*/
|
33
|
-
|
34
|
-
const validMethods = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT'];
|
35
|
-
|
36
|
-
/*
|
37
|
-
Copyright 2018 Google LLC
|
38
|
-
|
39
|
-
Use of this source code is governed by an MIT-style
|
40
|
-
license that can be found in the LICENSE file or at
|
41
|
-
https://opensource.org/licenses/MIT.
|
42
|
-
*/
|
43
|
-
/**
|
44
|
-
* @param {function()|Object} handler Either a function, or an object with a
|
45
|
-
* 'handle' method.
|
46
|
-
* @return {Object} An object with a handle method.
|
47
|
-
*
|
48
|
-
* @private
|
49
|
-
*/
|
50
|
-
|
51
|
-
const normalizeHandler = handler => {
|
52
|
-
if (handler && typeof handler === 'object') {
|
53
|
-
{
|
54
|
-
assert_js.assert.hasMethod(handler, 'handle', {
|
55
|
-
moduleName: 'workbox-routing',
|
56
|
-
className: 'Route',
|
57
|
-
funcName: 'constructor',
|
58
|
-
paramName: 'handler'
|
59
|
-
});
|
60
|
-
}
|
61
|
-
|
62
|
-
return handler;
|
63
|
-
} else {
|
64
|
-
{
|
65
|
-
assert_js.assert.isType(handler, 'function', {
|
66
|
-
moduleName: 'workbox-routing',
|
67
|
-
className: 'Route',
|
68
|
-
funcName: 'constructor',
|
69
|
-
paramName: 'handler'
|
70
|
-
});
|
71
|
-
}
|
72
|
-
|
73
|
-
return {
|
74
|
-
handle: handler
|
75
|
-
};
|
76
|
-
}
|
77
|
-
};
|
78
|
-
|
79
|
-
/*
|
80
|
-
Copyright 2018 Google LLC
|
81
|
-
|
82
|
-
Use of this source code is governed by an MIT-style
|
83
|
-
license that can be found in the LICENSE file or at
|
84
|
-
https://opensource.org/licenses/MIT.
|
85
|
-
*/
|
86
|
-
/**
|
87
|
-
* A `Route` consists of a pair of callback functions, "match" and "handler".
|
88
|
-
* The "match" callback determine if a route should be used to "handle" a
|
89
|
-
* request by returning a non-falsy value if it can. The "handler" callback
|
90
|
-
* is called when there is a match and should return a Promise that resolves
|
91
|
-
* to a `Response`.
|
92
|
-
*
|
93
|
-
* @memberof module:workbox-routing
|
94
|
-
*/
|
95
|
-
|
96
|
-
class Route {
|
97
|
-
/**
|
98
|
-
* Constructor for Route class.
|
99
|
-
*
|
100
|
-
* @param {module:workbox-routing~matchCallback} match
|
101
|
-
* A callback function that determines whether the route matches a given
|
102
|
-
* `fetch` event by returning a non-falsy value.
|
103
|
-
* @param {module:workbox-routing~handlerCallback} handler A callback
|
104
|
-
* function that returns a Promise resolving to a Response.
|
105
|
-
* @param {string} [method='GET'] The HTTP method to match the Route
|
106
|
-
* against.
|
107
|
-
*/
|
108
|
-
constructor(match, handler, method = defaultMethod) {
|
109
|
-
{
|
110
|
-
assert_js.assert.isType(match, 'function', {
|
111
|
-
moduleName: 'workbox-routing',
|
112
|
-
className: 'Route',
|
113
|
-
funcName: 'constructor',
|
114
|
-
paramName: 'match'
|
115
|
-
});
|
116
|
-
|
117
|
-
if (method) {
|
118
|
-
assert_js.assert.isOneOf(method, validMethods, {
|
119
|
-
paramName: 'method'
|
120
|
-
});
|
121
|
-
}
|
122
|
-
} // These values are referenced directly by Router so cannot be
|
123
|
-
// altered by minificaton.
|
124
|
-
|
125
|
-
|
126
|
-
this.handler = normalizeHandler(handler);
|
127
|
-
this.match = match;
|
128
|
-
this.method = method;
|
129
|
-
}
|
130
|
-
|
131
|
-
}
|
132
|
-
|
133
|
-
/*
|
134
|
-
Copyright 2018 Google LLC
|
135
|
-
|
136
|
-
Use of this source code is governed by an MIT-style
|
137
|
-
license that can be found in the LICENSE file or at
|
138
|
-
https://opensource.org/licenses/MIT.
|
139
|
-
*/
|
140
|
-
/**
|
141
|
-
* NavigationRoute makes it easy to create a
|
142
|
-
* [Route]{@link module:workbox-routing.Route} that matches for browser
|
143
|
-
* [navigation requests]{@link https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests}.
|
144
|
-
*
|
145
|
-
* It will only match incoming Requests whose
|
146
|
-
* [`mode`]{@link https://fetch.spec.whatwg.org/#concept-request-mode}
|
147
|
-
* is set to `navigate`.
|
148
|
-
*
|
149
|
-
* You can optionally only apply this route to a subset of navigation requests
|
150
|
-
* by using one or both of the `denylist` and `allowlist` parameters.
|
151
|
-
*
|
152
|
-
* @memberof module:workbox-routing
|
153
|
-
* @extends module:workbox-routing.Route
|
154
|
-
*/
|
155
|
-
|
156
|
-
class NavigationRoute extends Route {
|
157
|
-
/**
|
158
|
-
* If both `denylist` and `allowlist` are provided, the `denylist` will
|
159
|
-
* take precedence and the request will not match this route.
|
160
|
-
*
|
161
|
-
* The regular expressions in `allowlist` and `denylist`
|
162
|
-
* are matched against the concatenated
|
163
|
-
* [`pathname`]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/pathname}
|
164
|
-
* and [`search`]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search}
|
165
|
-
* portions of the requested URL.
|
166
|
-
*
|
167
|
-
* @param {module:workbox-routing~handlerCallback} handler A callback
|
168
|
-
* function that returns a Promise resulting in a Response.
|
169
|
-
* @param {Object} options
|
170
|
-
* @param {Array<RegExp>} [options.denylist] If any of these patterns match,
|
171
|
-
* the route will not handle the request (even if a allowlist RegExp matches).
|
172
|
-
* @param {Array<RegExp>} [options.allowlist=[/./]] If any of these patterns
|
173
|
-
* match the URL's pathname and search parameter, the route will handle the
|
174
|
-
* request (assuming the denylist doesn't match).
|
175
|
-
*/
|
176
|
-
constructor(handler, {
|
177
|
-
allowlist = [/./],
|
178
|
-
denylist = []
|
179
|
-
} = {}) {
|
180
|
-
{
|
181
|
-
assert_js.assert.isArrayOfClass(allowlist, RegExp, {
|
182
|
-
moduleName: 'workbox-routing',
|
183
|
-
className: 'NavigationRoute',
|
184
|
-
funcName: 'constructor',
|
185
|
-
paramName: 'options.allowlist'
|
186
|
-
});
|
187
|
-
assert_js.assert.isArrayOfClass(denylist, RegExp, {
|
188
|
-
moduleName: 'workbox-routing',
|
189
|
-
className: 'NavigationRoute',
|
190
|
-
funcName: 'constructor',
|
191
|
-
paramName: 'options.denylist'
|
192
|
-
});
|
193
|
-
}
|
194
|
-
|
195
|
-
super(options => this._match(options), handler);
|
196
|
-
this._allowlist = allowlist;
|
197
|
-
this._denylist = denylist;
|
198
|
-
}
|
199
|
-
/**
|
200
|
-
* Routes match handler.
|
201
|
-
*
|
202
|
-
* @param {Object} options
|
203
|
-
* @param {URL} options.url
|
204
|
-
* @param {Request} options.request
|
205
|
-
* @return {boolean}
|
206
|
-
*
|
207
|
-
* @private
|
208
|
-
*/
|
209
|
-
|
210
|
-
|
211
|
-
_match({
|
212
|
-
url,
|
213
|
-
request
|
214
|
-
}) {
|
215
|
-
if (request && request.mode !== 'navigate') {
|
216
|
-
return false;
|
217
|
-
}
|
218
|
-
|
219
|
-
const pathnameAndSearch = url.pathname + url.search;
|
220
|
-
|
221
|
-
for (const regExp of this._denylist) {
|
222
|
-
if (regExp.test(pathnameAndSearch)) {
|
223
|
-
{
|
224
|
-
logger_js.logger.log(`The navigation route ${pathnameAndSearch} is not ` + `being used, since the URL matches this denylist pattern: ` + `${regExp}`);
|
225
|
-
}
|
226
|
-
|
227
|
-
return false;
|
228
|
-
}
|
229
|
-
}
|
230
|
-
|
231
|
-
if (this._allowlist.some(regExp => regExp.test(pathnameAndSearch))) {
|
232
|
-
{
|
233
|
-
logger_js.logger.debug(`The navigation route ${pathnameAndSearch} ` + `is being used.`);
|
234
|
-
}
|
235
|
-
|
236
|
-
return true;
|
237
|
-
}
|
238
|
-
|
239
|
-
{
|
240
|
-
logger_js.logger.log(`The navigation route ${pathnameAndSearch} is not ` + `being used, since the URL being navigated to doesn't ` + `match the allowlist.`);
|
241
|
-
}
|
242
|
-
|
243
|
-
return false;
|
244
|
-
}
|
245
|
-
|
246
|
-
}
|
247
|
-
|
248
|
-
/*
|
249
|
-
Copyright 2018 Google LLC
|
250
|
-
|
251
|
-
Use of this source code is governed by an MIT-style
|
252
|
-
license that can be found in the LICENSE file or at
|
253
|
-
https://opensource.org/licenses/MIT.
|
254
|
-
*/
|
255
|
-
/**
|
256
|
-
* RegExpRoute makes it easy to create a regular expression based
|
257
|
-
* [Route]{@link module:workbox-routing.Route}.
|
258
|
-
*
|
259
|
-
* For same-origin requests the RegExp only needs to match part of the URL. For
|
260
|
-
* requests against third-party servers, you must define a RegExp that matches
|
261
|
-
* the start of the URL.
|
262
|
-
*
|
263
|
-
* [See the module docs for info.]{@link https://developers.google.com/web/tools/workbox/modules/workbox-routing}
|
264
|
-
*
|
265
|
-
* @memberof module:workbox-routing
|
266
|
-
* @extends module:workbox-routing.Route
|
267
|
-
*/
|
268
|
-
|
269
|
-
class RegExpRoute extends Route {
|
270
|
-
/**
|
271
|
-
* If the regular expression contains
|
272
|
-
* [capture groups]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references},
|
273
|
-
* the captured values will be passed to the
|
274
|
-
* [handler's]{@link module:workbox-routing~handlerCallback} `params`
|
275
|
-
* argument.
|
276
|
-
*
|
277
|
-
* @param {RegExp} regExp The regular expression to match against URLs.
|
278
|
-
* @param {module:workbox-routing~handlerCallback} handler A callback
|
279
|
-
* function that returns a Promise resulting in a Response.
|
280
|
-
* @param {string} [method='GET'] The HTTP method to match the Route
|
281
|
-
* against.
|
282
|
-
*/
|
283
|
-
constructor(regExp, handler, method) {
|
284
|
-
{
|
285
|
-
assert_js.assert.isInstance(regExp, RegExp, {
|
286
|
-
moduleName: 'workbox-routing',
|
287
|
-
className: 'RegExpRoute',
|
288
|
-
funcName: 'constructor',
|
289
|
-
paramName: 'pattern'
|
290
|
-
});
|
291
|
-
}
|
292
|
-
|
293
|
-
const match = ({
|
294
|
-
url
|
295
|
-
}) => {
|
296
|
-
const result = regExp.exec(url.href); // Return immediately if there's no match.
|
297
|
-
|
298
|
-
if (!result) {
|
299
|
-
return;
|
300
|
-
} // Require that the match start at the first character in the URL string
|
301
|
-
// if it's a cross-origin request.
|
302
|
-
// See https://github.com/GoogleChrome/workbox/issues/281 for the context
|
303
|
-
// behind this behavior.
|
304
|
-
|
305
|
-
|
306
|
-
if (url.origin !== location.origin && result.index !== 0) {
|
307
|
-
{
|
308
|
-
logger_js.logger.debug(`The regular expression '${regExp}' only partially matched ` + `against the cross-origin URL '${url}'. RegExpRoute's will only ` + `handle cross-origin requests if they match the entire URL.`);
|
309
|
-
}
|
310
|
-
|
311
|
-
return;
|
312
|
-
} // If the route matches, but there aren't any capture groups defined, then
|
313
|
-
// this will return [], which is truthy and therefore sufficient to
|
314
|
-
// indicate a match.
|
315
|
-
// If there are capture groups, then it will return their values.
|
316
|
-
|
317
|
-
|
318
|
-
return result.slice(1);
|
319
|
-
};
|
320
|
-
|
321
|
-
super(match, handler, method);
|
322
|
-
}
|
323
|
-
|
324
|
-
}
|
325
|
-
|
326
|
-
/*
|
327
|
-
Copyright 2018 Google LLC
|
328
|
-
|
329
|
-
Use of this source code is governed by an MIT-style
|
330
|
-
license that can be found in the LICENSE file or at
|
331
|
-
https://opensource.org/licenses/MIT.
|
332
|
-
*/
|
333
|
-
/**
|
334
|
-
* The Router can be used to process a FetchEvent through one or more
|
335
|
-
* [Routes]{@link module:workbox-routing.Route} responding with a Request if
|
336
|
-
* a matching route exists.
|
337
|
-
*
|
338
|
-
* If no route matches a given a request, the Router will use a "default"
|
339
|
-
* handler if one is defined.
|
340
|
-
*
|
341
|
-
* Should the matching Route throw an error, the Router will use a "catch"
|
342
|
-
* handler if one is defined to gracefully deal with issues and respond with a
|
343
|
-
* Request.
|
344
|
-
*
|
345
|
-
* If a request matches multiple routes, the **earliest** registered route will
|
346
|
-
* be used to respond to the request.
|
347
|
-
*
|
348
|
-
* @memberof module:workbox-routing
|
349
|
-
*/
|
350
|
-
|
351
|
-
class Router {
|
352
|
-
/**
|
353
|
-
* Initializes a new Router.
|
354
|
-
*/
|
355
|
-
constructor() {
|
356
|
-
this._routes = new Map();
|
357
|
-
}
|
358
|
-
/**
|
359
|
-
* @return {Map<string, Array<module:workbox-routing.Route>>} routes A `Map` of HTTP
|
360
|
-
* method name ('GET', etc.) to an array of all the corresponding `Route`
|
361
|
-
* instances that are registered.
|
362
|
-
*/
|
363
|
-
|
364
|
-
|
365
|
-
get routes() {
|
366
|
-
return this._routes;
|
367
|
-
}
|
368
|
-
/**
|
369
|
-
* Adds a fetch event listener to respond to events when a route matches
|
370
|
-
* the event's request.
|
371
|
-
*/
|
372
|
-
|
373
|
-
|
374
|
-
addFetchListener() {
|
375
|
-
// See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
|
376
|
-
self.addEventListener('fetch', event => {
|
377
|
-
const {
|
378
|
-
request
|
379
|
-
} = event;
|
380
|
-
const responsePromise = this.handleRequest({
|
381
|
-
request,
|
382
|
-
event
|
383
|
-
});
|
384
|
-
|
385
|
-
if (responsePromise) {
|
386
|
-
event.respondWith(responsePromise);
|
387
|
-
}
|
388
|
-
});
|
389
|
-
}
|
390
|
-
/**
|
391
|
-
* Adds a message event listener for URLs to cache from the window.
|
392
|
-
* This is useful to cache resources loaded on the page prior to when the
|
393
|
-
* service worker started controlling it.
|
394
|
-
*
|
395
|
-
* The format of the message data sent from the window should be as follows.
|
396
|
-
* Where the `urlsToCache` array may consist of URL strings or an array of
|
397
|
-
* URL string + `requestInit` object (the same as you'd pass to `fetch()`).
|
398
|
-
*
|
399
|
-
* ```
|
400
|
-
* {
|
401
|
-
* type: 'CACHE_URLS',
|
402
|
-
* payload: {
|
403
|
-
* urlsToCache: [
|
404
|
-
* './script1.js',
|
405
|
-
* './script2.js',
|
406
|
-
* ['./script3.js', {mode: 'no-cors'}],
|
407
|
-
* ],
|
408
|
-
* },
|
409
|
-
* }
|
410
|
-
* ```
|
411
|
-
*/
|
412
|
-
|
413
|
-
|
414
|
-
addCacheListener() {
|
415
|
-
// See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
|
416
|
-
self.addEventListener('message', event => {
|
417
|
-
if (event.data && event.data.type === 'CACHE_URLS') {
|
418
|
-
const {
|
419
|
-
payload
|
420
|
-
} = event.data;
|
421
|
-
|
422
|
-
{
|
423
|
-
logger_js.logger.debug(`Caching URLs from the window`, payload.urlsToCache);
|
424
|
-
}
|
425
|
-
|
426
|
-
const requestPromises = Promise.all(payload.urlsToCache.map(entry => {
|
427
|
-
if (typeof entry === 'string') {
|
428
|
-
entry = [entry];
|
429
|
-
}
|
430
|
-
|
431
|
-
const request = new Request(...entry);
|
432
|
-
return this.handleRequest({
|
433
|
-
request
|
434
|
-
}); // TODO(philipwalton): TypeScript errors without this typecast for
|
435
|
-
// some reason (probably a bug). The real type here should work but
|
436
|
-
// doesn't: `Array<Promise<Response> | undefined>`.
|
437
|
-
})); // TypeScript
|
438
|
-
|
439
|
-
event.waitUntil(requestPromises); // If a MessageChannel was used, reply to the message on success.
|
440
|
-
|
441
|
-
if (event.ports && event.ports[0]) {
|
442
|
-
requestPromises.then(() => event.ports[0].postMessage(true));
|
443
|
-
}
|
444
|
-
}
|
445
|
-
});
|
446
|
-
}
|
447
|
-
/**
|
448
|
-
* Apply the routing rules to a FetchEvent object to get a Response from an
|
449
|
-
* appropriate Route's handler.
|
450
|
-
*
|
451
|
-
* @param {Object} options
|
452
|
-
* @param {Request} options.request The request to handle (this is usually
|
453
|
-
* from a fetch event, but it does not have to be).
|
454
|
-
* @param {FetchEvent} [options.event] The event that triggered the request,
|
455
|
-
* if applicable.
|
456
|
-
* @return {Promise<Response>|undefined} A promise is returned if a
|
457
|
-
* registered route can handle the request. If there is no matching
|
458
|
-
* route and there's no `defaultHandler`, `undefined` is returned.
|
459
|
-
*/
|
460
|
-
|
461
|
-
|
462
|
-
handleRequest({
|
463
|
-
request,
|
464
|
-
event
|
465
|
-
}) {
|
466
|
-
{
|
467
|
-
assert_js.assert.isInstance(request, Request, {
|
468
|
-
moduleName: 'workbox-routing',
|
469
|
-
className: 'Router',
|
470
|
-
funcName: 'handleRequest',
|
471
|
-
paramName: 'options.request'
|
472
|
-
});
|
473
|
-
}
|
474
|
-
|
475
|
-
const url = new URL(request.url, location.href);
|
476
|
-
|
477
|
-
if (!url.protocol.startsWith('http')) {
|
478
|
-
{
|
479
|
-
logger_js.logger.debug(`Workbox Router only supports URLs that start with 'http'.`);
|
480
|
-
}
|
481
|
-
|
482
|
-
return;
|
483
|
-
}
|
484
|
-
|
485
|
-
const {
|
486
|
-
params,
|
487
|
-
route
|
488
|
-
} = this.findMatchingRoute({
|
489
|
-
url,
|
490
|
-
request,
|
491
|
-
event
|
492
|
-
});
|
493
|
-
let handler = route && route.handler;
|
494
|
-
const debugMessages = [];
|
495
|
-
|
496
|
-
{
|
497
|
-
if (handler) {
|
498
|
-
debugMessages.push([`Found a route to handle this request:`, route]);
|
499
|
-
|
500
|
-
if (params) {
|
501
|
-
debugMessages.push([`Passing the following params to the route's handler:`, params]);
|
502
|
-
}
|
503
|
-
}
|
504
|
-
} // If we don't have a handler because there was no matching route, then
|
505
|
-
// fall back to defaultHandler if that's defined.
|
506
|
-
|
507
|
-
|
508
|
-
if (!handler && this._defaultHandler) {
|
509
|
-
{
|
510
|
-
debugMessages.push(`Failed to find a matching route. Falling ` + `back to the default handler.`);
|
511
|
-
}
|
512
|
-
|
513
|
-
handler = this._defaultHandler;
|
514
|
-
}
|
515
|
-
|
516
|
-
if (!handler) {
|
517
|
-
{
|
518
|
-
// No handler so Workbox will do nothing. If logs is set of debug
|
519
|
-
// i.e. verbose, we should print out this information.
|
520
|
-
logger_js.logger.debug(`No route found for: ${getFriendlyURL_js.getFriendlyURL(url)}`);
|
521
|
-
}
|
522
|
-
|
523
|
-
return;
|
524
|
-
}
|
525
|
-
|
526
|
-
{
|
527
|
-
// We have a handler, meaning Workbox is going to handle the route.
|
528
|
-
// print the routing details to the console.
|
529
|
-
logger_js.logger.groupCollapsed(`Router is responding to: ${getFriendlyURL_js.getFriendlyURL(url)}`);
|
530
|
-
debugMessages.forEach(msg => {
|
531
|
-
if (Array.isArray(msg)) {
|
532
|
-
logger_js.logger.log(...msg);
|
533
|
-
} else {
|
534
|
-
logger_js.logger.log(msg);
|
535
|
-
}
|
536
|
-
});
|
537
|
-
logger_js.logger.groupEnd();
|
538
|
-
} // Wrap in try and catch in case the handle method throws a synchronous
|
539
|
-
// error. It should still callback to the catch handler.
|
540
|
-
|
541
|
-
|
542
|
-
let responsePromise;
|
543
|
-
|
544
|
-
try {
|
545
|
-
responsePromise = handler.handle({
|
546
|
-
url,
|
547
|
-
request,
|
548
|
-
event,
|
549
|
-
params
|
550
|
-
});
|
551
|
-
} catch (err) {
|
552
|
-
responsePromise = Promise.reject(err);
|
553
|
-
}
|
554
|
-
|
555
|
-
if (responsePromise instanceof Promise && this._catchHandler) {
|
556
|
-
responsePromise = responsePromise.catch(err => {
|
557
|
-
{
|
558
|
-
// Still include URL here as it will be async from the console group
|
559
|
-
// and may not make sense without the URL
|
560
|
-
logger_js.logger.groupCollapsed(`Error thrown when responding to: ` + ` ${getFriendlyURL_js.getFriendlyURL(url)}. Falling back to Catch Handler.`);
|
561
|
-
logger_js.logger.error(`Error thrown by:`, route);
|
562
|
-
logger_js.logger.error(err);
|
563
|
-
logger_js.logger.groupEnd();
|
564
|
-
}
|
565
|
-
|
566
|
-
return this._catchHandler.handle({
|
567
|
-
url,
|
568
|
-
request,
|
569
|
-
event
|
570
|
-
});
|
571
|
-
});
|
572
|
-
}
|
573
|
-
|
574
|
-
return responsePromise;
|
575
|
-
}
|
576
|
-
/**
|
577
|
-
* Checks a request and URL (and optionally an event) against the list of
|
578
|
-
* registered routes, and if there's a match, returns the corresponding
|
579
|
-
* route along with any params generated by the match.
|
580
|
-
*
|
581
|
-
* @param {Object} options
|
582
|
-
* @param {URL} options.url
|
583
|
-
* @param {Request} options.request The request to match.
|
584
|
-
* @param {Event} [options.event] The corresponding event (unless N/A).
|
585
|
-
* @return {Object} An object with `route` and `params` properties.
|
586
|
-
* They are populated if a matching route was found or `undefined`
|
587
|
-
* otherwise.
|
588
|
-
*/
|
589
|
-
|
590
|
-
|
591
|
-
findMatchingRoute({
|
592
|
-
url,
|
593
|
-
request,
|
594
|
-
event
|
595
|
-
}) {
|
596
|
-
{
|
597
|
-
assert_js.assert.isInstance(url, URL, {
|
598
|
-
moduleName: 'workbox-routing',
|
599
|
-
className: 'Router',
|
600
|
-
funcName: 'findMatchingRoute',
|
601
|
-
paramName: 'options.url'
|
602
|
-
});
|
603
|
-
assert_js.assert.isInstance(request, Request, {
|
604
|
-
moduleName: 'workbox-routing',
|
605
|
-
className: 'Router',
|
606
|
-
funcName: 'findMatchingRoute',
|
607
|
-
paramName: 'options.request'
|
608
|
-
});
|
609
|
-
}
|
610
|
-
|
611
|
-
const routes = this._routes.get(request.method) || [];
|
612
|
-
|
613
|
-
for (const route of routes) {
|
614
|
-
let params;
|
615
|
-
const matchResult = route.match({
|
616
|
-
url,
|
617
|
-
request,
|
618
|
-
event
|
619
|
-
});
|
620
|
-
|
621
|
-
if (matchResult) {
|
622
|
-
// See https://github.com/GoogleChrome/workbox/issues/2079
|
623
|
-
params = matchResult;
|
624
|
-
|
625
|
-
if (Array.isArray(matchResult) && matchResult.length === 0) {
|
626
|
-
// Instead of passing an empty array in as params, use undefined.
|
627
|
-
params = undefined;
|
628
|
-
} else if (matchResult.constructor === Object && Object.keys(matchResult).length === 0) {
|
629
|
-
// Instead of passing an empty object in as params, use undefined.
|
630
|
-
params = undefined;
|
631
|
-
} else if (typeof matchResult === 'boolean') {
|
632
|
-
// For the boolean value true (rather than just something truth-y),
|
633
|
-
// don't set params.
|
634
|
-
// See https://github.com/GoogleChrome/workbox/pull/2134#issuecomment-513924353
|
635
|
-
params = undefined;
|
636
|
-
} // Return early if have a match.
|
637
|
-
|
638
|
-
|
639
|
-
return {
|
640
|
-
route,
|
641
|
-
params
|
642
|
-
};
|
643
|
-
}
|
644
|
-
} // If no match was found above, return and empty object.
|
645
|
-
|
646
|
-
|
647
|
-
return {};
|
648
|
-
}
|
649
|
-
/**
|
650
|
-
* Define a default `handler` that's called when no routes explicitly
|
651
|
-
* match the incoming request.
|
652
|
-
*
|
653
|
-
* Without a default handler, unmatched requests will go against the
|
654
|
-
* network as if there were no service worker present.
|
655
|
-
*
|
656
|
-
* @param {module:workbox-routing~handlerCallback} handler A callback
|
657
|
-
* function that returns a Promise resulting in a Response.
|
658
|
-
*/
|
659
|
-
|
660
|
-
|
661
|
-
setDefaultHandler(handler) {
|
662
|
-
this._defaultHandler = normalizeHandler(handler);
|
663
|
-
}
|
664
|
-
/**
|
665
|
-
* If a Route throws an error while handling a request, this `handler`
|
666
|
-
* will be called and given a chance to provide a response.
|
667
|
-
*
|
668
|
-
* @param {module:workbox-routing~handlerCallback} handler A callback
|
669
|
-
* function that returns a Promise resulting in a Response.
|
670
|
-
*/
|
671
|
-
|
672
|
-
|
673
|
-
setCatchHandler(handler) {
|
674
|
-
this._catchHandler = normalizeHandler(handler);
|
675
|
-
}
|
676
|
-
/**
|
677
|
-
* Registers a route with the router.
|
678
|
-
*
|
679
|
-
* @param {module:workbox-routing.Route} route The route to register.
|
680
|
-
*/
|
681
|
-
|
682
|
-
|
683
|
-
registerRoute(route) {
|
684
|
-
{
|
685
|
-
assert_js.assert.isType(route, 'object', {
|
686
|
-
moduleName: 'workbox-routing',
|
687
|
-
className: 'Router',
|
688
|
-
funcName: 'registerRoute',
|
689
|
-
paramName: 'route'
|
690
|
-
});
|
691
|
-
assert_js.assert.hasMethod(route, 'match', {
|
692
|
-
moduleName: 'workbox-routing',
|
693
|
-
className: 'Router',
|
694
|
-
funcName: 'registerRoute',
|
695
|
-
paramName: 'route'
|
696
|
-
});
|
697
|
-
assert_js.assert.isType(route.handler, 'object', {
|
698
|
-
moduleName: 'workbox-routing',
|
699
|
-
className: 'Router',
|
700
|
-
funcName: 'registerRoute',
|
701
|
-
paramName: 'route'
|
702
|
-
});
|
703
|
-
assert_js.assert.hasMethod(route.handler, 'handle', {
|
704
|
-
moduleName: 'workbox-routing',
|
705
|
-
className: 'Router',
|
706
|
-
funcName: 'registerRoute',
|
707
|
-
paramName: 'route.handler'
|
708
|
-
});
|
709
|
-
assert_js.assert.isType(route.method, 'string', {
|
710
|
-
moduleName: 'workbox-routing',
|
711
|
-
className: 'Router',
|
712
|
-
funcName: 'registerRoute',
|
713
|
-
paramName: 'route.method'
|
714
|
-
});
|
715
|
-
}
|
716
|
-
|
717
|
-
if (!this._routes.has(route.method)) {
|
718
|
-
this._routes.set(route.method, []);
|
719
|
-
} // Give precedence to all of the earlier routes by adding this additional
|
720
|
-
// route to the end of the array.
|
721
|
-
|
722
|
-
|
723
|
-
this._routes.get(route.method).push(route);
|
724
|
-
}
|
725
|
-
/**
|
726
|
-
* Unregisters a route with the router.
|
727
|
-
*
|
728
|
-
* @param {module:workbox-routing.Route} route The route to unregister.
|
729
|
-
*/
|
730
|
-
|
731
|
-
|
732
|
-
unregisterRoute(route) {
|
733
|
-
if (!this._routes.has(route.method)) {
|
734
|
-
throw new WorkboxError_js.WorkboxError('unregister-route-but-not-found-with-method', {
|
735
|
-
method: route.method
|
736
|
-
});
|
737
|
-
}
|
738
|
-
|
739
|
-
const routeIndex = this._routes.get(route.method).indexOf(route);
|
740
|
-
|
741
|
-
if (routeIndex > -1) {
|
742
|
-
this._routes.get(route.method).splice(routeIndex, 1);
|
743
|
-
} else {
|
744
|
-
throw new WorkboxError_js.WorkboxError('unregister-route-route-not-registered');
|
745
|
-
}
|
746
|
-
}
|
747
|
-
|
748
|
-
}
|
749
|
-
|
750
|
-
/*
|
751
|
-
Copyright 2019 Google LLC
|
752
|
-
|
753
|
-
Use of this source code is governed by an MIT-style
|
754
|
-
license that can be found in the LICENSE file or at
|
755
|
-
https://opensource.org/licenses/MIT.
|
756
|
-
*/
|
757
|
-
let defaultRouter;
|
758
|
-
/**
|
759
|
-
* Creates a new, singleton Router instance if one does not exist. If one
|
760
|
-
* does already exist, that instance is returned.
|
761
|
-
*
|
762
|
-
* @private
|
763
|
-
* @return {Router}
|
764
|
-
*/
|
765
|
-
|
766
|
-
const getOrCreateDefaultRouter = () => {
|
767
|
-
if (!defaultRouter) {
|
768
|
-
defaultRouter = new Router(); // The helpers that use the default Router assume these listeners exist.
|
769
|
-
|
770
|
-
defaultRouter.addFetchListener();
|
771
|
-
defaultRouter.addCacheListener();
|
772
|
-
}
|
773
|
-
|
774
|
-
return defaultRouter;
|
775
|
-
};
|
776
|
-
|
777
|
-
/*
|
778
|
-
Copyright 2019 Google LLC
|
779
|
-
|
780
|
-
Use of this source code is governed by an MIT-style
|
781
|
-
license that can be found in the LICENSE file or at
|
782
|
-
https://opensource.org/licenses/MIT.
|
783
|
-
*/
|
784
|
-
/**
|
785
|
-
* Easily register a RegExp, string, or function with a caching
|
786
|
-
* strategy to a singleton Router instance.
|
787
|
-
*
|
788
|
-
* This method will generate a Route for you if needed and
|
789
|
-
* call [registerRoute()]{@link module:workbox-routing.Router#registerRoute}.
|
790
|
-
*
|
791
|
-
* @param {RegExp|string|module:workbox-routing.Route~matchCallback|module:workbox-routing.Route} capture
|
792
|
-
* If the capture param is a `Route`, all other arguments will be ignored.
|
793
|
-
* @param {module:workbox-routing~handlerCallback} [handler] A callback
|
794
|
-
* function that returns a Promise resulting in a Response. This parameter
|
795
|
-
* is required if `capture` is not a `Route` object.
|
796
|
-
* @param {string} [method='GET'] The HTTP method to match the Route
|
797
|
-
* against.
|
798
|
-
* @return {module:workbox-routing.Route} The generated `Route`(Useful for
|
799
|
-
* unregistering).
|
800
|
-
*
|
801
|
-
* @memberof module:workbox-routing
|
802
|
-
*/
|
803
|
-
|
804
|
-
function registerRoute(capture, handler, method) {
|
805
|
-
let route;
|
806
|
-
|
807
|
-
if (typeof capture === 'string') {
|
808
|
-
const captureUrl = new URL(capture, location.href);
|
809
|
-
|
810
|
-
{
|
811
|
-
if (!(capture.startsWith('/') || capture.startsWith('http'))) {
|
812
|
-
throw new WorkboxError_js.WorkboxError('invalid-string', {
|
813
|
-
moduleName: 'workbox-routing',
|
814
|
-
funcName: 'registerRoute',
|
815
|
-
paramName: 'capture'
|
816
|
-
});
|
817
|
-
} // We want to check if Express-style wildcards are in the pathname only.
|
818
|
-
// TODO: Remove this log message in v4.
|
819
|
-
|
820
|
-
|
821
|
-
const valueToCheck = capture.startsWith('http') ? captureUrl.pathname : capture; // See https://github.com/pillarjs/path-to-regexp#parameters
|
822
|
-
|
823
|
-
const wildcards = '[*:?+]';
|
824
|
-
|
825
|
-
if (new RegExp(`${wildcards}`).exec(valueToCheck)) {
|
826
|
-
logger_js.logger.debug(`The '$capture' parameter contains an Express-style wildcard ` + `character (${wildcards}). Strings are now always interpreted as ` + `exact matches; use a RegExp for partial or wildcard matches.`);
|
827
|
-
}
|
828
|
-
}
|
829
|
-
|
830
|
-
const matchCallback = ({
|
831
|
-
url
|
832
|
-
}) => {
|
833
|
-
{
|
834
|
-
if (url.pathname === captureUrl.pathname && url.origin !== captureUrl.origin) {
|
835
|
-
logger_js.logger.debug(`${capture} only partially matches the cross-origin URL ` + `${url}. This route will only handle cross-origin requests ` + `if they match the entire URL.`);
|
836
|
-
}
|
837
|
-
}
|
838
|
-
|
839
|
-
return url.href === captureUrl.href;
|
840
|
-
}; // If `capture` is a string then `handler` and `method` must be present.
|
841
|
-
|
842
|
-
|
843
|
-
route = new Route(matchCallback, handler, method);
|
844
|
-
} else if (capture instanceof RegExp) {
|
845
|
-
// If `capture` is a `RegExp` then `handler` and `method` must be present.
|
846
|
-
route = new RegExpRoute(capture, handler, method);
|
847
|
-
} else if (typeof capture === 'function') {
|
848
|
-
// If `capture` is a function then `handler` and `method` must be present.
|
849
|
-
route = new Route(capture, handler, method);
|
850
|
-
} else if (capture instanceof Route) {
|
851
|
-
route = capture;
|
852
|
-
} else {
|
853
|
-
throw new WorkboxError_js.WorkboxError('unsupported-route-type', {
|
854
|
-
moduleName: 'workbox-routing',
|
855
|
-
funcName: 'registerRoute',
|
856
|
-
paramName: 'capture'
|
857
|
-
});
|
858
|
-
}
|
859
|
-
|
860
|
-
const defaultRouter = getOrCreateDefaultRouter();
|
861
|
-
defaultRouter.registerRoute(route);
|
862
|
-
return route;
|
863
|
-
}
|
864
|
-
|
865
|
-
/*
|
866
|
-
Copyright 2019 Google LLC
|
867
|
-
|
868
|
-
Use of this source code is governed by an MIT-style
|
869
|
-
license that can be found in the LICENSE file or at
|
870
|
-
https://opensource.org/licenses/MIT.
|
871
|
-
*/
|
872
|
-
/**
|
873
|
-
* If a Route throws an error while handling a request, this `handler`
|
874
|
-
* will be called and given a chance to provide a response.
|
875
|
-
*
|
876
|
-
* @param {module:workbox-routing~handlerCallback} handler A callback
|
877
|
-
* function that returns a Promise resulting in a Response.
|
878
|
-
*
|
879
|
-
* @memberof module:workbox-routing
|
880
|
-
*/
|
881
|
-
|
882
|
-
function setCatchHandler(handler) {
|
883
|
-
const defaultRouter = getOrCreateDefaultRouter();
|
884
|
-
defaultRouter.setCatchHandler(handler);
|
885
|
-
}
|
886
|
-
|
887
|
-
/*
|
888
|
-
Copyright 2019 Google LLC
|
889
|
-
|
890
|
-
Use of this source code is governed by an MIT-style
|
891
|
-
license that can be found in the LICENSE file or at
|
892
|
-
https://opensource.org/licenses/MIT.
|
893
|
-
*/
|
894
|
-
/**
|
895
|
-
* Define a default `handler` that's called when no routes explicitly
|
896
|
-
* match the incoming request.
|
897
|
-
*
|
898
|
-
* Without a default handler, unmatched requests will go against the
|
899
|
-
* network as if there were no service worker present.
|
900
|
-
*
|
901
|
-
* @param {module:workbox-routing~handlerCallback} handler A callback
|
902
|
-
* function that returns a Promise resulting in a Response.
|
903
|
-
*
|
904
|
-
* @memberof module:workbox-routing
|
905
|
-
*/
|
906
|
-
|
907
|
-
function setDefaultHandler(handler) {
|
908
|
-
const defaultRouter = getOrCreateDefaultRouter();
|
909
|
-
defaultRouter.setDefaultHandler(handler);
|
910
|
-
}
|
911
|
-
|
912
|
-
exports.NavigationRoute = NavigationRoute;
|
913
|
-
exports.RegExpRoute = RegExpRoute;
|
914
|
-
exports.Route = Route;
|
915
|
-
exports.Router = Router;
|
916
|
-
exports.registerRoute = registerRoute;
|
917
|
-
exports.setCatchHandler = setCatchHandler;
|
918
|
-
exports.setDefaultHandler = setDefaultHandler;
|
919
|
-
|
920
|
-
return exports;
|
921
|
-
|
922
|
-
}({}, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private));
|
923
|
-
//# sourceMappingURL=workbox-routing.dev.js.map
|
1
|
+
this.workbox = this.workbox || {};
|
2
|
+
this.workbox.routing = (function (exports, assert_js, logger_js, WorkboxError_js, getFriendlyURL_js) {
|
3
|
+
'use strict';
|
4
|
+
|
5
|
+
try {
|
6
|
+
self['workbox:routing:5.1.4'] && _();
|
7
|
+
} catch (e) {}
|
8
|
+
|
9
|
+
/*
|
10
|
+
Copyright 2018 Google LLC
|
11
|
+
|
12
|
+
Use of this source code is governed by an MIT-style
|
13
|
+
license that can be found in the LICENSE file or at
|
14
|
+
https://opensource.org/licenses/MIT.
|
15
|
+
*/
|
16
|
+
/**
|
17
|
+
* The default HTTP method, 'GET', used when there's no specific method
|
18
|
+
* configured for a route.
|
19
|
+
*
|
20
|
+
* @type {string}
|
21
|
+
*
|
22
|
+
* @private
|
23
|
+
*/
|
24
|
+
|
25
|
+
const defaultMethod = 'GET';
|
26
|
+
/**
|
27
|
+
* The list of valid HTTP methods associated with requests that could be routed.
|
28
|
+
*
|
29
|
+
* @type {Array<string>}
|
30
|
+
*
|
31
|
+
* @private
|
32
|
+
*/
|
33
|
+
|
34
|
+
const validMethods = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT'];
|
35
|
+
|
36
|
+
/*
|
37
|
+
Copyright 2018 Google LLC
|
38
|
+
|
39
|
+
Use of this source code is governed by an MIT-style
|
40
|
+
license that can be found in the LICENSE file or at
|
41
|
+
https://opensource.org/licenses/MIT.
|
42
|
+
*/
|
43
|
+
/**
|
44
|
+
* @param {function()|Object} handler Either a function, or an object with a
|
45
|
+
* 'handle' method.
|
46
|
+
* @return {Object} An object with a handle method.
|
47
|
+
*
|
48
|
+
* @private
|
49
|
+
*/
|
50
|
+
|
51
|
+
const normalizeHandler = handler => {
|
52
|
+
if (handler && typeof handler === 'object') {
|
53
|
+
{
|
54
|
+
assert_js.assert.hasMethod(handler, 'handle', {
|
55
|
+
moduleName: 'workbox-routing',
|
56
|
+
className: 'Route',
|
57
|
+
funcName: 'constructor',
|
58
|
+
paramName: 'handler'
|
59
|
+
});
|
60
|
+
}
|
61
|
+
|
62
|
+
return handler;
|
63
|
+
} else {
|
64
|
+
{
|
65
|
+
assert_js.assert.isType(handler, 'function', {
|
66
|
+
moduleName: 'workbox-routing',
|
67
|
+
className: 'Route',
|
68
|
+
funcName: 'constructor',
|
69
|
+
paramName: 'handler'
|
70
|
+
});
|
71
|
+
}
|
72
|
+
|
73
|
+
return {
|
74
|
+
handle: handler
|
75
|
+
};
|
76
|
+
}
|
77
|
+
};
|
78
|
+
|
79
|
+
/*
|
80
|
+
Copyright 2018 Google LLC
|
81
|
+
|
82
|
+
Use of this source code is governed by an MIT-style
|
83
|
+
license that can be found in the LICENSE file or at
|
84
|
+
https://opensource.org/licenses/MIT.
|
85
|
+
*/
|
86
|
+
/**
|
87
|
+
* A `Route` consists of a pair of callback functions, "match" and "handler".
|
88
|
+
* The "match" callback determine if a route should be used to "handle" a
|
89
|
+
* request by returning a non-falsy value if it can. The "handler" callback
|
90
|
+
* is called when there is a match and should return a Promise that resolves
|
91
|
+
* to a `Response`.
|
92
|
+
*
|
93
|
+
* @memberof module:workbox-routing
|
94
|
+
*/
|
95
|
+
|
96
|
+
class Route {
|
97
|
+
/**
|
98
|
+
* Constructor for Route class.
|
99
|
+
*
|
100
|
+
* @param {module:workbox-routing~matchCallback} match
|
101
|
+
* A callback function that determines whether the route matches a given
|
102
|
+
* `fetch` event by returning a non-falsy value.
|
103
|
+
* @param {module:workbox-routing~handlerCallback} handler A callback
|
104
|
+
* function that returns a Promise resolving to a Response.
|
105
|
+
* @param {string} [method='GET'] The HTTP method to match the Route
|
106
|
+
* against.
|
107
|
+
*/
|
108
|
+
constructor(match, handler, method = defaultMethod) {
|
109
|
+
{
|
110
|
+
assert_js.assert.isType(match, 'function', {
|
111
|
+
moduleName: 'workbox-routing',
|
112
|
+
className: 'Route',
|
113
|
+
funcName: 'constructor',
|
114
|
+
paramName: 'match'
|
115
|
+
});
|
116
|
+
|
117
|
+
if (method) {
|
118
|
+
assert_js.assert.isOneOf(method, validMethods, {
|
119
|
+
paramName: 'method'
|
120
|
+
});
|
121
|
+
}
|
122
|
+
} // These values are referenced directly by Router so cannot be
|
123
|
+
// altered by minificaton.
|
124
|
+
|
125
|
+
|
126
|
+
this.handler = normalizeHandler(handler);
|
127
|
+
this.match = match;
|
128
|
+
this.method = method;
|
129
|
+
}
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
/*
|
134
|
+
Copyright 2018 Google LLC
|
135
|
+
|
136
|
+
Use of this source code is governed by an MIT-style
|
137
|
+
license that can be found in the LICENSE file or at
|
138
|
+
https://opensource.org/licenses/MIT.
|
139
|
+
*/
|
140
|
+
/**
|
141
|
+
* NavigationRoute makes it easy to create a
|
142
|
+
* [Route]{@link module:workbox-routing.Route} that matches for browser
|
143
|
+
* [navigation requests]{@link https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests}.
|
144
|
+
*
|
145
|
+
* It will only match incoming Requests whose
|
146
|
+
* [`mode`]{@link https://fetch.spec.whatwg.org/#concept-request-mode}
|
147
|
+
* is set to `navigate`.
|
148
|
+
*
|
149
|
+
* You can optionally only apply this route to a subset of navigation requests
|
150
|
+
* by using one or both of the `denylist` and `allowlist` parameters.
|
151
|
+
*
|
152
|
+
* @memberof module:workbox-routing
|
153
|
+
* @extends module:workbox-routing.Route
|
154
|
+
*/
|
155
|
+
|
156
|
+
class NavigationRoute extends Route {
|
157
|
+
/**
|
158
|
+
* If both `denylist` and `allowlist` are provided, the `denylist` will
|
159
|
+
* take precedence and the request will not match this route.
|
160
|
+
*
|
161
|
+
* The regular expressions in `allowlist` and `denylist`
|
162
|
+
* are matched against the concatenated
|
163
|
+
* [`pathname`]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/pathname}
|
164
|
+
* and [`search`]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search}
|
165
|
+
* portions of the requested URL.
|
166
|
+
*
|
167
|
+
* @param {module:workbox-routing~handlerCallback} handler A callback
|
168
|
+
* function that returns a Promise resulting in a Response.
|
169
|
+
* @param {Object} options
|
170
|
+
* @param {Array<RegExp>} [options.denylist] If any of these patterns match,
|
171
|
+
* the route will not handle the request (even if a allowlist RegExp matches).
|
172
|
+
* @param {Array<RegExp>} [options.allowlist=[/./]] If any of these patterns
|
173
|
+
* match the URL's pathname and search parameter, the route will handle the
|
174
|
+
* request (assuming the denylist doesn't match).
|
175
|
+
*/
|
176
|
+
constructor(handler, {
|
177
|
+
allowlist = [/./],
|
178
|
+
denylist = []
|
179
|
+
} = {}) {
|
180
|
+
{
|
181
|
+
assert_js.assert.isArrayOfClass(allowlist, RegExp, {
|
182
|
+
moduleName: 'workbox-routing',
|
183
|
+
className: 'NavigationRoute',
|
184
|
+
funcName: 'constructor',
|
185
|
+
paramName: 'options.allowlist'
|
186
|
+
});
|
187
|
+
assert_js.assert.isArrayOfClass(denylist, RegExp, {
|
188
|
+
moduleName: 'workbox-routing',
|
189
|
+
className: 'NavigationRoute',
|
190
|
+
funcName: 'constructor',
|
191
|
+
paramName: 'options.denylist'
|
192
|
+
});
|
193
|
+
}
|
194
|
+
|
195
|
+
super(options => this._match(options), handler);
|
196
|
+
this._allowlist = allowlist;
|
197
|
+
this._denylist = denylist;
|
198
|
+
}
|
199
|
+
/**
|
200
|
+
* Routes match handler.
|
201
|
+
*
|
202
|
+
* @param {Object} options
|
203
|
+
* @param {URL} options.url
|
204
|
+
* @param {Request} options.request
|
205
|
+
* @return {boolean}
|
206
|
+
*
|
207
|
+
* @private
|
208
|
+
*/
|
209
|
+
|
210
|
+
|
211
|
+
_match({
|
212
|
+
url,
|
213
|
+
request
|
214
|
+
}) {
|
215
|
+
if (request && request.mode !== 'navigate') {
|
216
|
+
return false;
|
217
|
+
}
|
218
|
+
|
219
|
+
const pathnameAndSearch = url.pathname + url.search;
|
220
|
+
|
221
|
+
for (const regExp of this._denylist) {
|
222
|
+
if (regExp.test(pathnameAndSearch)) {
|
223
|
+
{
|
224
|
+
logger_js.logger.log(`The navigation route ${pathnameAndSearch} is not ` + `being used, since the URL matches this denylist pattern: ` + `${regExp}`);
|
225
|
+
}
|
226
|
+
|
227
|
+
return false;
|
228
|
+
}
|
229
|
+
}
|
230
|
+
|
231
|
+
if (this._allowlist.some(regExp => regExp.test(pathnameAndSearch))) {
|
232
|
+
{
|
233
|
+
logger_js.logger.debug(`The navigation route ${pathnameAndSearch} ` + `is being used.`);
|
234
|
+
}
|
235
|
+
|
236
|
+
return true;
|
237
|
+
}
|
238
|
+
|
239
|
+
{
|
240
|
+
logger_js.logger.log(`The navigation route ${pathnameAndSearch} is not ` + `being used, since the URL being navigated to doesn't ` + `match the allowlist.`);
|
241
|
+
}
|
242
|
+
|
243
|
+
return false;
|
244
|
+
}
|
245
|
+
|
246
|
+
}
|
247
|
+
|
248
|
+
/*
|
249
|
+
Copyright 2018 Google LLC
|
250
|
+
|
251
|
+
Use of this source code is governed by an MIT-style
|
252
|
+
license that can be found in the LICENSE file or at
|
253
|
+
https://opensource.org/licenses/MIT.
|
254
|
+
*/
|
255
|
+
/**
|
256
|
+
* RegExpRoute makes it easy to create a regular expression based
|
257
|
+
* [Route]{@link module:workbox-routing.Route}.
|
258
|
+
*
|
259
|
+
* For same-origin requests the RegExp only needs to match part of the URL. For
|
260
|
+
* requests against third-party servers, you must define a RegExp that matches
|
261
|
+
* the start of the URL.
|
262
|
+
*
|
263
|
+
* [See the module docs for info.]{@link https://developers.google.com/web/tools/workbox/modules/workbox-routing}
|
264
|
+
*
|
265
|
+
* @memberof module:workbox-routing
|
266
|
+
* @extends module:workbox-routing.Route
|
267
|
+
*/
|
268
|
+
|
269
|
+
class RegExpRoute extends Route {
|
270
|
+
/**
|
271
|
+
* If the regular expression contains
|
272
|
+
* [capture groups]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references},
|
273
|
+
* the captured values will be passed to the
|
274
|
+
* [handler's]{@link module:workbox-routing~handlerCallback} `params`
|
275
|
+
* argument.
|
276
|
+
*
|
277
|
+
* @param {RegExp} regExp The regular expression to match against URLs.
|
278
|
+
* @param {module:workbox-routing~handlerCallback} handler A callback
|
279
|
+
* function that returns a Promise resulting in a Response.
|
280
|
+
* @param {string} [method='GET'] The HTTP method to match the Route
|
281
|
+
* against.
|
282
|
+
*/
|
283
|
+
constructor(regExp, handler, method) {
|
284
|
+
{
|
285
|
+
assert_js.assert.isInstance(regExp, RegExp, {
|
286
|
+
moduleName: 'workbox-routing',
|
287
|
+
className: 'RegExpRoute',
|
288
|
+
funcName: 'constructor',
|
289
|
+
paramName: 'pattern'
|
290
|
+
});
|
291
|
+
}
|
292
|
+
|
293
|
+
const match = ({
|
294
|
+
url
|
295
|
+
}) => {
|
296
|
+
const result = regExp.exec(url.href); // Return immediately if there's no match.
|
297
|
+
|
298
|
+
if (!result) {
|
299
|
+
return;
|
300
|
+
} // Require that the match start at the first character in the URL string
|
301
|
+
// if it's a cross-origin request.
|
302
|
+
// See https://github.com/GoogleChrome/workbox/issues/281 for the context
|
303
|
+
// behind this behavior.
|
304
|
+
|
305
|
+
|
306
|
+
if (url.origin !== location.origin && result.index !== 0) {
|
307
|
+
{
|
308
|
+
logger_js.logger.debug(`The regular expression '${regExp}' only partially matched ` + `against the cross-origin URL '${url}'. RegExpRoute's will only ` + `handle cross-origin requests if they match the entire URL.`);
|
309
|
+
}
|
310
|
+
|
311
|
+
return;
|
312
|
+
} // If the route matches, but there aren't any capture groups defined, then
|
313
|
+
// this will return [], which is truthy and therefore sufficient to
|
314
|
+
// indicate a match.
|
315
|
+
// If there are capture groups, then it will return their values.
|
316
|
+
|
317
|
+
|
318
|
+
return result.slice(1);
|
319
|
+
};
|
320
|
+
|
321
|
+
super(match, handler, method);
|
322
|
+
}
|
323
|
+
|
324
|
+
}
|
325
|
+
|
326
|
+
/*
|
327
|
+
Copyright 2018 Google LLC
|
328
|
+
|
329
|
+
Use of this source code is governed by an MIT-style
|
330
|
+
license that can be found in the LICENSE file or at
|
331
|
+
https://opensource.org/licenses/MIT.
|
332
|
+
*/
|
333
|
+
/**
|
334
|
+
* The Router can be used to process a FetchEvent through one or more
|
335
|
+
* [Routes]{@link module:workbox-routing.Route} responding with a Request if
|
336
|
+
* a matching route exists.
|
337
|
+
*
|
338
|
+
* If no route matches a given a request, the Router will use a "default"
|
339
|
+
* handler if one is defined.
|
340
|
+
*
|
341
|
+
* Should the matching Route throw an error, the Router will use a "catch"
|
342
|
+
* handler if one is defined to gracefully deal with issues and respond with a
|
343
|
+
* Request.
|
344
|
+
*
|
345
|
+
* If a request matches multiple routes, the **earliest** registered route will
|
346
|
+
* be used to respond to the request.
|
347
|
+
*
|
348
|
+
* @memberof module:workbox-routing
|
349
|
+
*/
|
350
|
+
|
351
|
+
class Router {
|
352
|
+
/**
|
353
|
+
* Initializes a new Router.
|
354
|
+
*/
|
355
|
+
constructor() {
|
356
|
+
this._routes = new Map();
|
357
|
+
}
|
358
|
+
/**
|
359
|
+
* @return {Map<string, Array<module:workbox-routing.Route>>} routes A `Map` of HTTP
|
360
|
+
* method name ('GET', etc.) to an array of all the corresponding `Route`
|
361
|
+
* instances that are registered.
|
362
|
+
*/
|
363
|
+
|
364
|
+
|
365
|
+
get routes() {
|
366
|
+
return this._routes;
|
367
|
+
}
|
368
|
+
/**
|
369
|
+
* Adds a fetch event listener to respond to events when a route matches
|
370
|
+
* the event's request.
|
371
|
+
*/
|
372
|
+
|
373
|
+
|
374
|
+
addFetchListener() {
|
375
|
+
// See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
|
376
|
+
self.addEventListener('fetch', event => {
|
377
|
+
const {
|
378
|
+
request
|
379
|
+
} = event;
|
380
|
+
const responsePromise = this.handleRequest({
|
381
|
+
request,
|
382
|
+
event
|
383
|
+
});
|
384
|
+
|
385
|
+
if (responsePromise) {
|
386
|
+
event.respondWith(responsePromise);
|
387
|
+
}
|
388
|
+
});
|
389
|
+
}
|
390
|
+
/**
|
391
|
+
* Adds a message event listener for URLs to cache from the window.
|
392
|
+
* This is useful to cache resources loaded on the page prior to when the
|
393
|
+
* service worker started controlling it.
|
394
|
+
*
|
395
|
+
* The format of the message data sent from the window should be as follows.
|
396
|
+
* Where the `urlsToCache` array may consist of URL strings or an array of
|
397
|
+
* URL string + `requestInit` object (the same as you'd pass to `fetch()`).
|
398
|
+
*
|
399
|
+
* ```
|
400
|
+
* {
|
401
|
+
* type: 'CACHE_URLS',
|
402
|
+
* payload: {
|
403
|
+
* urlsToCache: [
|
404
|
+
* './script1.js',
|
405
|
+
* './script2.js',
|
406
|
+
* ['./script3.js', {mode: 'no-cors'}],
|
407
|
+
* ],
|
408
|
+
* },
|
409
|
+
* }
|
410
|
+
* ```
|
411
|
+
*/
|
412
|
+
|
413
|
+
|
414
|
+
addCacheListener() {
|
415
|
+
// See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
|
416
|
+
self.addEventListener('message', event => {
|
417
|
+
if (event.data && event.data.type === 'CACHE_URLS') {
|
418
|
+
const {
|
419
|
+
payload
|
420
|
+
} = event.data;
|
421
|
+
|
422
|
+
{
|
423
|
+
logger_js.logger.debug(`Caching URLs from the window`, payload.urlsToCache);
|
424
|
+
}
|
425
|
+
|
426
|
+
const requestPromises = Promise.all(payload.urlsToCache.map(entry => {
|
427
|
+
if (typeof entry === 'string') {
|
428
|
+
entry = [entry];
|
429
|
+
}
|
430
|
+
|
431
|
+
const request = new Request(...entry);
|
432
|
+
return this.handleRequest({
|
433
|
+
request
|
434
|
+
}); // TODO(philipwalton): TypeScript errors without this typecast for
|
435
|
+
// some reason (probably a bug). The real type here should work but
|
436
|
+
// doesn't: `Array<Promise<Response> | undefined>`.
|
437
|
+
})); // TypeScript
|
438
|
+
|
439
|
+
event.waitUntil(requestPromises); // If a MessageChannel was used, reply to the message on success.
|
440
|
+
|
441
|
+
if (event.ports && event.ports[0]) {
|
442
|
+
requestPromises.then(() => event.ports[0].postMessage(true));
|
443
|
+
}
|
444
|
+
}
|
445
|
+
});
|
446
|
+
}
|
447
|
+
/**
|
448
|
+
* Apply the routing rules to a FetchEvent object to get a Response from an
|
449
|
+
* appropriate Route's handler.
|
450
|
+
*
|
451
|
+
* @param {Object} options
|
452
|
+
* @param {Request} options.request The request to handle (this is usually
|
453
|
+
* from a fetch event, but it does not have to be).
|
454
|
+
* @param {FetchEvent} [options.event] The event that triggered the request,
|
455
|
+
* if applicable.
|
456
|
+
* @return {Promise<Response>|undefined} A promise is returned if a
|
457
|
+
* registered route can handle the request. If there is no matching
|
458
|
+
* route and there's no `defaultHandler`, `undefined` is returned.
|
459
|
+
*/
|
460
|
+
|
461
|
+
|
462
|
+
handleRequest({
|
463
|
+
request,
|
464
|
+
event
|
465
|
+
}) {
|
466
|
+
{
|
467
|
+
assert_js.assert.isInstance(request, Request, {
|
468
|
+
moduleName: 'workbox-routing',
|
469
|
+
className: 'Router',
|
470
|
+
funcName: 'handleRequest',
|
471
|
+
paramName: 'options.request'
|
472
|
+
});
|
473
|
+
}
|
474
|
+
|
475
|
+
const url = new URL(request.url, location.href);
|
476
|
+
|
477
|
+
if (!url.protocol.startsWith('http')) {
|
478
|
+
{
|
479
|
+
logger_js.logger.debug(`Workbox Router only supports URLs that start with 'http'.`);
|
480
|
+
}
|
481
|
+
|
482
|
+
return;
|
483
|
+
}
|
484
|
+
|
485
|
+
const {
|
486
|
+
params,
|
487
|
+
route
|
488
|
+
} = this.findMatchingRoute({
|
489
|
+
url,
|
490
|
+
request,
|
491
|
+
event
|
492
|
+
});
|
493
|
+
let handler = route && route.handler;
|
494
|
+
const debugMessages = [];
|
495
|
+
|
496
|
+
{
|
497
|
+
if (handler) {
|
498
|
+
debugMessages.push([`Found a route to handle this request:`, route]);
|
499
|
+
|
500
|
+
if (params) {
|
501
|
+
debugMessages.push([`Passing the following params to the route's handler:`, params]);
|
502
|
+
}
|
503
|
+
}
|
504
|
+
} // If we don't have a handler because there was no matching route, then
|
505
|
+
// fall back to defaultHandler if that's defined.
|
506
|
+
|
507
|
+
|
508
|
+
if (!handler && this._defaultHandler) {
|
509
|
+
{
|
510
|
+
debugMessages.push(`Failed to find a matching route. Falling ` + `back to the default handler.`);
|
511
|
+
}
|
512
|
+
|
513
|
+
handler = this._defaultHandler;
|
514
|
+
}
|
515
|
+
|
516
|
+
if (!handler) {
|
517
|
+
{
|
518
|
+
// No handler so Workbox will do nothing. If logs is set of debug
|
519
|
+
// i.e. verbose, we should print out this information.
|
520
|
+
logger_js.logger.debug(`No route found for: ${getFriendlyURL_js.getFriendlyURL(url)}`);
|
521
|
+
}
|
522
|
+
|
523
|
+
return;
|
524
|
+
}
|
525
|
+
|
526
|
+
{
|
527
|
+
// We have a handler, meaning Workbox is going to handle the route.
|
528
|
+
// print the routing details to the console.
|
529
|
+
logger_js.logger.groupCollapsed(`Router is responding to: ${getFriendlyURL_js.getFriendlyURL(url)}`);
|
530
|
+
debugMessages.forEach(msg => {
|
531
|
+
if (Array.isArray(msg)) {
|
532
|
+
logger_js.logger.log(...msg);
|
533
|
+
} else {
|
534
|
+
logger_js.logger.log(msg);
|
535
|
+
}
|
536
|
+
});
|
537
|
+
logger_js.logger.groupEnd();
|
538
|
+
} // Wrap in try and catch in case the handle method throws a synchronous
|
539
|
+
// error. It should still callback to the catch handler.
|
540
|
+
|
541
|
+
|
542
|
+
let responsePromise;
|
543
|
+
|
544
|
+
try {
|
545
|
+
responsePromise = handler.handle({
|
546
|
+
url,
|
547
|
+
request,
|
548
|
+
event,
|
549
|
+
params
|
550
|
+
});
|
551
|
+
} catch (err) {
|
552
|
+
responsePromise = Promise.reject(err);
|
553
|
+
}
|
554
|
+
|
555
|
+
if (responsePromise instanceof Promise && this._catchHandler) {
|
556
|
+
responsePromise = responsePromise.catch(err => {
|
557
|
+
{
|
558
|
+
// Still include URL here as it will be async from the console group
|
559
|
+
// and may not make sense without the URL
|
560
|
+
logger_js.logger.groupCollapsed(`Error thrown when responding to: ` + ` ${getFriendlyURL_js.getFriendlyURL(url)}. Falling back to Catch Handler.`);
|
561
|
+
logger_js.logger.error(`Error thrown by:`, route);
|
562
|
+
logger_js.logger.error(err);
|
563
|
+
logger_js.logger.groupEnd();
|
564
|
+
}
|
565
|
+
|
566
|
+
return this._catchHandler.handle({
|
567
|
+
url,
|
568
|
+
request,
|
569
|
+
event
|
570
|
+
});
|
571
|
+
});
|
572
|
+
}
|
573
|
+
|
574
|
+
return responsePromise;
|
575
|
+
}
|
576
|
+
/**
|
577
|
+
* Checks a request and URL (and optionally an event) against the list of
|
578
|
+
* registered routes, and if there's a match, returns the corresponding
|
579
|
+
* route along with any params generated by the match.
|
580
|
+
*
|
581
|
+
* @param {Object} options
|
582
|
+
* @param {URL} options.url
|
583
|
+
* @param {Request} options.request The request to match.
|
584
|
+
* @param {Event} [options.event] The corresponding event (unless N/A).
|
585
|
+
* @return {Object} An object with `route` and `params` properties.
|
586
|
+
* They are populated if a matching route was found or `undefined`
|
587
|
+
* otherwise.
|
588
|
+
*/
|
589
|
+
|
590
|
+
|
591
|
+
findMatchingRoute({
|
592
|
+
url,
|
593
|
+
request,
|
594
|
+
event
|
595
|
+
}) {
|
596
|
+
{
|
597
|
+
assert_js.assert.isInstance(url, URL, {
|
598
|
+
moduleName: 'workbox-routing',
|
599
|
+
className: 'Router',
|
600
|
+
funcName: 'findMatchingRoute',
|
601
|
+
paramName: 'options.url'
|
602
|
+
});
|
603
|
+
assert_js.assert.isInstance(request, Request, {
|
604
|
+
moduleName: 'workbox-routing',
|
605
|
+
className: 'Router',
|
606
|
+
funcName: 'findMatchingRoute',
|
607
|
+
paramName: 'options.request'
|
608
|
+
});
|
609
|
+
}
|
610
|
+
|
611
|
+
const routes = this._routes.get(request.method) || [];
|
612
|
+
|
613
|
+
for (const route of routes) {
|
614
|
+
let params;
|
615
|
+
const matchResult = route.match({
|
616
|
+
url,
|
617
|
+
request,
|
618
|
+
event
|
619
|
+
});
|
620
|
+
|
621
|
+
if (matchResult) {
|
622
|
+
// See https://github.com/GoogleChrome/workbox/issues/2079
|
623
|
+
params = matchResult;
|
624
|
+
|
625
|
+
if (Array.isArray(matchResult) && matchResult.length === 0) {
|
626
|
+
// Instead of passing an empty array in as params, use undefined.
|
627
|
+
params = undefined;
|
628
|
+
} else if (matchResult.constructor === Object && Object.keys(matchResult).length === 0) {
|
629
|
+
// Instead of passing an empty object in as params, use undefined.
|
630
|
+
params = undefined;
|
631
|
+
} else if (typeof matchResult === 'boolean') {
|
632
|
+
// For the boolean value true (rather than just something truth-y),
|
633
|
+
// don't set params.
|
634
|
+
// See https://github.com/GoogleChrome/workbox/pull/2134#issuecomment-513924353
|
635
|
+
params = undefined;
|
636
|
+
} // Return early if have a match.
|
637
|
+
|
638
|
+
|
639
|
+
return {
|
640
|
+
route,
|
641
|
+
params
|
642
|
+
};
|
643
|
+
}
|
644
|
+
} // If no match was found above, return and empty object.
|
645
|
+
|
646
|
+
|
647
|
+
return {};
|
648
|
+
}
|
649
|
+
/**
|
650
|
+
* Define a default `handler` that's called when no routes explicitly
|
651
|
+
* match the incoming request.
|
652
|
+
*
|
653
|
+
* Without a default handler, unmatched requests will go against the
|
654
|
+
* network as if there were no service worker present.
|
655
|
+
*
|
656
|
+
* @param {module:workbox-routing~handlerCallback} handler A callback
|
657
|
+
* function that returns a Promise resulting in a Response.
|
658
|
+
*/
|
659
|
+
|
660
|
+
|
661
|
+
setDefaultHandler(handler) {
|
662
|
+
this._defaultHandler = normalizeHandler(handler);
|
663
|
+
}
|
664
|
+
/**
|
665
|
+
* If a Route throws an error while handling a request, this `handler`
|
666
|
+
* will be called and given a chance to provide a response.
|
667
|
+
*
|
668
|
+
* @param {module:workbox-routing~handlerCallback} handler A callback
|
669
|
+
* function that returns a Promise resulting in a Response.
|
670
|
+
*/
|
671
|
+
|
672
|
+
|
673
|
+
setCatchHandler(handler) {
|
674
|
+
this._catchHandler = normalizeHandler(handler);
|
675
|
+
}
|
676
|
+
/**
|
677
|
+
* Registers a route with the router.
|
678
|
+
*
|
679
|
+
* @param {module:workbox-routing.Route} route The route to register.
|
680
|
+
*/
|
681
|
+
|
682
|
+
|
683
|
+
registerRoute(route) {
|
684
|
+
{
|
685
|
+
assert_js.assert.isType(route, 'object', {
|
686
|
+
moduleName: 'workbox-routing',
|
687
|
+
className: 'Router',
|
688
|
+
funcName: 'registerRoute',
|
689
|
+
paramName: 'route'
|
690
|
+
});
|
691
|
+
assert_js.assert.hasMethod(route, 'match', {
|
692
|
+
moduleName: 'workbox-routing',
|
693
|
+
className: 'Router',
|
694
|
+
funcName: 'registerRoute',
|
695
|
+
paramName: 'route'
|
696
|
+
});
|
697
|
+
assert_js.assert.isType(route.handler, 'object', {
|
698
|
+
moduleName: 'workbox-routing',
|
699
|
+
className: 'Router',
|
700
|
+
funcName: 'registerRoute',
|
701
|
+
paramName: 'route'
|
702
|
+
});
|
703
|
+
assert_js.assert.hasMethod(route.handler, 'handle', {
|
704
|
+
moduleName: 'workbox-routing',
|
705
|
+
className: 'Router',
|
706
|
+
funcName: 'registerRoute',
|
707
|
+
paramName: 'route.handler'
|
708
|
+
});
|
709
|
+
assert_js.assert.isType(route.method, 'string', {
|
710
|
+
moduleName: 'workbox-routing',
|
711
|
+
className: 'Router',
|
712
|
+
funcName: 'registerRoute',
|
713
|
+
paramName: 'route.method'
|
714
|
+
});
|
715
|
+
}
|
716
|
+
|
717
|
+
if (!this._routes.has(route.method)) {
|
718
|
+
this._routes.set(route.method, []);
|
719
|
+
} // Give precedence to all of the earlier routes by adding this additional
|
720
|
+
// route to the end of the array.
|
721
|
+
|
722
|
+
|
723
|
+
this._routes.get(route.method).push(route);
|
724
|
+
}
|
725
|
+
/**
|
726
|
+
* Unregisters a route with the router.
|
727
|
+
*
|
728
|
+
* @param {module:workbox-routing.Route} route The route to unregister.
|
729
|
+
*/
|
730
|
+
|
731
|
+
|
732
|
+
unregisterRoute(route) {
|
733
|
+
if (!this._routes.has(route.method)) {
|
734
|
+
throw new WorkboxError_js.WorkboxError('unregister-route-but-not-found-with-method', {
|
735
|
+
method: route.method
|
736
|
+
});
|
737
|
+
}
|
738
|
+
|
739
|
+
const routeIndex = this._routes.get(route.method).indexOf(route);
|
740
|
+
|
741
|
+
if (routeIndex > -1) {
|
742
|
+
this._routes.get(route.method).splice(routeIndex, 1);
|
743
|
+
} else {
|
744
|
+
throw new WorkboxError_js.WorkboxError('unregister-route-route-not-registered');
|
745
|
+
}
|
746
|
+
}
|
747
|
+
|
748
|
+
}
|
749
|
+
|
750
|
+
/*
|
751
|
+
Copyright 2019 Google LLC
|
752
|
+
|
753
|
+
Use of this source code is governed by an MIT-style
|
754
|
+
license that can be found in the LICENSE file or at
|
755
|
+
https://opensource.org/licenses/MIT.
|
756
|
+
*/
|
757
|
+
let defaultRouter;
|
758
|
+
/**
|
759
|
+
* Creates a new, singleton Router instance if one does not exist. If one
|
760
|
+
* does already exist, that instance is returned.
|
761
|
+
*
|
762
|
+
* @private
|
763
|
+
* @return {Router}
|
764
|
+
*/
|
765
|
+
|
766
|
+
const getOrCreateDefaultRouter = () => {
|
767
|
+
if (!defaultRouter) {
|
768
|
+
defaultRouter = new Router(); // The helpers that use the default Router assume these listeners exist.
|
769
|
+
|
770
|
+
defaultRouter.addFetchListener();
|
771
|
+
defaultRouter.addCacheListener();
|
772
|
+
}
|
773
|
+
|
774
|
+
return defaultRouter;
|
775
|
+
};
|
776
|
+
|
777
|
+
/*
|
778
|
+
Copyright 2019 Google LLC
|
779
|
+
|
780
|
+
Use of this source code is governed by an MIT-style
|
781
|
+
license that can be found in the LICENSE file or at
|
782
|
+
https://opensource.org/licenses/MIT.
|
783
|
+
*/
|
784
|
+
/**
|
785
|
+
* Easily register a RegExp, string, or function with a caching
|
786
|
+
* strategy to a singleton Router instance.
|
787
|
+
*
|
788
|
+
* This method will generate a Route for you if needed and
|
789
|
+
* call [registerRoute()]{@link module:workbox-routing.Router#registerRoute}.
|
790
|
+
*
|
791
|
+
* @param {RegExp|string|module:workbox-routing.Route~matchCallback|module:workbox-routing.Route} capture
|
792
|
+
* If the capture param is a `Route`, all other arguments will be ignored.
|
793
|
+
* @param {module:workbox-routing~handlerCallback} [handler] A callback
|
794
|
+
* function that returns a Promise resulting in a Response. This parameter
|
795
|
+
* is required if `capture` is not a `Route` object.
|
796
|
+
* @param {string} [method='GET'] The HTTP method to match the Route
|
797
|
+
* against.
|
798
|
+
* @return {module:workbox-routing.Route} The generated `Route`(Useful for
|
799
|
+
* unregistering).
|
800
|
+
*
|
801
|
+
* @memberof module:workbox-routing
|
802
|
+
*/
|
803
|
+
|
804
|
+
function registerRoute(capture, handler, method) {
|
805
|
+
let route;
|
806
|
+
|
807
|
+
if (typeof capture === 'string') {
|
808
|
+
const captureUrl = new URL(capture, location.href);
|
809
|
+
|
810
|
+
{
|
811
|
+
if (!(capture.startsWith('/') || capture.startsWith('http'))) {
|
812
|
+
throw new WorkboxError_js.WorkboxError('invalid-string', {
|
813
|
+
moduleName: 'workbox-routing',
|
814
|
+
funcName: 'registerRoute',
|
815
|
+
paramName: 'capture'
|
816
|
+
});
|
817
|
+
} // We want to check if Express-style wildcards are in the pathname only.
|
818
|
+
// TODO: Remove this log message in v4.
|
819
|
+
|
820
|
+
|
821
|
+
const valueToCheck = capture.startsWith('http') ? captureUrl.pathname : capture; // See https://github.com/pillarjs/path-to-regexp#parameters
|
822
|
+
|
823
|
+
const wildcards = '[*:?+]';
|
824
|
+
|
825
|
+
if (new RegExp(`${wildcards}`).exec(valueToCheck)) {
|
826
|
+
logger_js.logger.debug(`The '$capture' parameter contains an Express-style wildcard ` + `character (${wildcards}). Strings are now always interpreted as ` + `exact matches; use a RegExp for partial or wildcard matches.`);
|
827
|
+
}
|
828
|
+
}
|
829
|
+
|
830
|
+
const matchCallback = ({
|
831
|
+
url
|
832
|
+
}) => {
|
833
|
+
{
|
834
|
+
if (url.pathname === captureUrl.pathname && url.origin !== captureUrl.origin) {
|
835
|
+
logger_js.logger.debug(`${capture} only partially matches the cross-origin URL ` + `${url}. This route will only handle cross-origin requests ` + `if they match the entire URL.`);
|
836
|
+
}
|
837
|
+
}
|
838
|
+
|
839
|
+
return url.href === captureUrl.href;
|
840
|
+
}; // If `capture` is a string then `handler` and `method` must be present.
|
841
|
+
|
842
|
+
|
843
|
+
route = new Route(matchCallback, handler, method);
|
844
|
+
} else if (capture instanceof RegExp) {
|
845
|
+
// If `capture` is a `RegExp` then `handler` and `method` must be present.
|
846
|
+
route = new RegExpRoute(capture, handler, method);
|
847
|
+
} else if (typeof capture === 'function') {
|
848
|
+
// If `capture` is a function then `handler` and `method` must be present.
|
849
|
+
route = new Route(capture, handler, method);
|
850
|
+
} else if (capture instanceof Route) {
|
851
|
+
route = capture;
|
852
|
+
} else {
|
853
|
+
throw new WorkboxError_js.WorkboxError('unsupported-route-type', {
|
854
|
+
moduleName: 'workbox-routing',
|
855
|
+
funcName: 'registerRoute',
|
856
|
+
paramName: 'capture'
|
857
|
+
});
|
858
|
+
}
|
859
|
+
|
860
|
+
const defaultRouter = getOrCreateDefaultRouter();
|
861
|
+
defaultRouter.registerRoute(route);
|
862
|
+
return route;
|
863
|
+
}
|
864
|
+
|
865
|
+
/*
|
866
|
+
Copyright 2019 Google LLC
|
867
|
+
|
868
|
+
Use of this source code is governed by an MIT-style
|
869
|
+
license that can be found in the LICENSE file or at
|
870
|
+
https://opensource.org/licenses/MIT.
|
871
|
+
*/
|
872
|
+
/**
|
873
|
+
* If a Route throws an error while handling a request, this `handler`
|
874
|
+
* will be called and given a chance to provide a response.
|
875
|
+
*
|
876
|
+
* @param {module:workbox-routing~handlerCallback} handler A callback
|
877
|
+
* function that returns a Promise resulting in a Response.
|
878
|
+
*
|
879
|
+
* @memberof module:workbox-routing
|
880
|
+
*/
|
881
|
+
|
882
|
+
function setCatchHandler(handler) {
|
883
|
+
const defaultRouter = getOrCreateDefaultRouter();
|
884
|
+
defaultRouter.setCatchHandler(handler);
|
885
|
+
}
|
886
|
+
|
887
|
+
/*
|
888
|
+
Copyright 2019 Google LLC
|
889
|
+
|
890
|
+
Use of this source code is governed by an MIT-style
|
891
|
+
license that can be found in the LICENSE file or at
|
892
|
+
https://opensource.org/licenses/MIT.
|
893
|
+
*/
|
894
|
+
/**
|
895
|
+
* Define a default `handler` that's called when no routes explicitly
|
896
|
+
* match the incoming request.
|
897
|
+
*
|
898
|
+
* Without a default handler, unmatched requests will go against the
|
899
|
+
* network as if there were no service worker present.
|
900
|
+
*
|
901
|
+
* @param {module:workbox-routing~handlerCallback} handler A callback
|
902
|
+
* function that returns a Promise resulting in a Response.
|
903
|
+
*
|
904
|
+
* @memberof module:workbox-routing
|
905
|
+
*/
|
906
|
+
|
907
|
+
function setDefaultHandler(handler) {
|
908
|
+
const defaultRouter = getOrCreateDefaultRouter();
|
909
|
+
defaultRouter.setDefaultHandler(handler);
|
910
|
+
}
|
911
|
+
|
912
|
+
exports.NavigationRoute = NavigationRoute;
|
913
|
+
exports.RegExpRoute = RegExpRoute;
|
914
|
+
exports.Route = Route;
|
915
|
+
exports.Router = Router;
|
916
|
+
exports.registerRoute = registerRoute;
|
917
|
+
exports.setCatchHandler = setCatchHandler;
|
918
|
+
exports.setDefaultHandler = setDefaultHandler;
|
919
|
+
|
920
|
+
return exports;
|
921
|
+
|
922
|
+
}({}, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private));
|
923
|
+
//# sourceMappingURL=workbox-routing.dev.js.map
|