| 1 | /*
|
|---|
| 2 | Copyright 2018 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 | import { assert } from 'workbox-core/_private/assert.js';
|
|---|
| 9 | import { logger } from 'workbox-core/_private/logger.js';
|
|---|
| 10 | import { Route } from './Route.js';
|
|---|
| 11 | import './_version.js';
|
|---|
| 12 | /**
|
|---|
| 13 | * RegExpRoute makes it easy to create a regular expression based
|
|---|
| 14 | * {@link workbox-routing.Route}.
|
|---|
| 15 | *
|
|---|
| 16 | * For same-origin requests the RegExp only needs to match part of the URL. For
|
|---|
| 17 | * requests against third-party servers, you must define a RegExp that matches
|
|---|
| 18 | * the start of the URL.
|
|---|
| 19 | *
|
|---|
| 20 | * @memberof workbox-routing
|
|---|
| 21 | * @extends workbox-routing.Route
|
|---|
| 22 | */
|
|---|
| 23 | class RegExpRoute extends Route {
|
|---|
| 24 | /**
|
|---|
| 25 | * If the regular expression contains
|
|---|
| 26 | * [capture groups]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references},
|
|---|
| 27 | * the captured values will be passed to the
|
|---|
| 28 | * {@link workbox-routing~handlerCallback} `params`
|
|---|
| 29 | * argument.
|
|---|
| 30 | *
|
|---|
| 31 | * @param {RegExp} regExp The regular expression to match against URLs.
|
|---|
| 32 | * @param {workbox-routing~handlerCallback} handler A callback
|
|---|
| 33 | * function that returns a Promise resulting in a Response.
|
|---|
| 34 | * @param {string} [method='GET'] The HTTP method to match the Route
|
|---|
| 35 | * against.
|
|---|
| 36 | */
|
|---|
| 37 | constructor(regExp, handler, method) {
|
|---|
| 38 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 39 | assert.isInstance(regExp, RegExp, {
|
|---|
| 40 | moduleName: 'workbox-routing',
|
|---|
| 41 | className: 'RegExpRoute',
|
|---|
| 42 | funcName: 'constructor',
|
|---|
| 43 | paramName: 'pattern',
|
|---|
| 44 | });
|
|---|
| 45 | }
|
|---|
| 46 | const match = ({ url }) => {
|
|---|
| 47 | const result = regExp.exec(url.href);
|
|---|
| 48 | // Return immediately if there's no match.
|
|---|
| 49 | if (!result) {
|
|---|
| 50 | return;
|
|---|
| 51 | }
|
|---|
| 52 | // Require that the match start at the first character in the URL string
|
|---|
| 53 | // if it's a cross-origin request.
|
|---|
| 54 | // See https://github.com/GoogleChrome/workbox/issues/281 for the context
|
|---|
| 55 | // behind this behavior.
|
|---|
| 56 | if (url.origin !== location.origin && result.index !== 0) {
|
|---|
| 57 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 58 | logger.debug(`The regular expression '${regExp.toString()}' only partially matched ` +
|
|---|
| 59 | `against the cross-origin URL '${url.toString()}'. RegExpRoute's will only ` +
|
|---|
| 60 | `handle cross-origin requests if they match the entire URL.`);
|
|---|
| 61 | }
|
|---|
| 62 | return;
|
|---|
| 63 | }
|
|---|
| 64 | // If the route matches, but there aren't any capture groups defined, then
|
|---|
| 65 | // this will return [], which is truthy and therefore sufficient to
|
|---|
| 66 | // indicate a match.
|
|---|
| 67 | // If there are capture groups, then it will return their values.
|
|---|
| 68 | return result.slice(1);
|
|---|
| 69 | };
|
|---|
| 70 | super(match, handler, method);
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | export { RegExpRoute };
|
|---|