RegExpRoute.js 3.0 KB

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