RegExpRoute.ts 3.1 KB

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