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