RegExpRoute.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 { Route } from './Route.js';
  10. import './_version.js';
  11. /**
  12. * RegExpRoute makes it easy to create a regular expression based
  13. * [Route]{@link module: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 module:workbox-routing
  22. * @extends module:workbox-routing.Route
  23. */
  24. class RegExpRoute extends Route {
  25. /**
  26. * If the regular expression contains
  27. * [capture groups]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references},
  28. * the captured values will be passed to the
  29. * [handler's]{@link module:workbox-routing~handlerCallback} `params`
  30. * argument.
  31. *
  32. * @param {RegExp} regExp The regular expression to match against URLs.
  33. * @param {module:workbox-routing~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 immediately if there's no match.
  50. if (!result) {
  51. return;
  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(`The regular expression '${regExp}' only partially matched ` +
  60. `against the cross-origin URL '${url}'. RegExpRoute's will only ` +
  61. `handle cross-origin requests if they match the entire URL.`);
  62. }
  63. return;
  64. }
  65. // If the route matches, but there aren't any capture groups defined, then
  66. // this will return [], which is truthy and therefore sufficient to
  67. // indicate a match.
  68. // If there are capture groups, then it will return their values.
  69. return result.slice(1);
  70. };
  71. super(match, handler, method);
  72. }
  73. }
  74. export { RegExpRoute };