RegExpRoute.mjs 2.9 KB

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