You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegExpRoute.mjs 3.3 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2017 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import {assert} from 'workbox-core/_private/assert.mjs';
  14. import {logger} from 'workbox-core/_private/logger.mjs';
  15. import {Route} from './Route.mjs';
  16. import './_version.mjs';
  17. /**
  18. * RegExpRoute makes it easy to create a regular expression based
  19. * [Route]{@link workbox.routing.Route}.
  20. *
  21. * For same-origin requests the RegExp only needs to match part of the URL. For
  22. * requests against third-party servers, you must define a RegExp that matches
  23. * the start of the URL.
  24. *
  25. * [See the module docs for info.]{@link https://developers.google.com/web/tools/workbox/modules/workbox-routing}
  26. *
  27. * @memberof workbox.routing
  28. * @extends workbox.routing.Route
  29. */
  30. class RegExpRoute extends Route {
  31. /**
  32. * If the regulard expression contains
  33. * [capture groups]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references},
  34. * th ecaptured values will be passed to the
  35. * [handler's]{@link workbox.routing.Route~handlerCallback} `params`
  36. * argument.
  37. *
  38. * @param {RegExp} regExp The regular expression to match against URLs.
  39. * @param {workbox.routing.Route~handlerCallback} handler A callback
  40. * function that returns a Promise resulting in a Response.
  41. * @param {string} [method='GET'] The HTTP method to match the Route
  42. * against.
  43. */
  44. constructor(regExp, handler, method) {
  45. if (process.env.NODE_ENV !== 'production') {
  46. assert.isInstance(regExp, RegExp, {
  47. moduleName: 'workbox-routing',
  48. className: 'RegExpRoute',
  49. funcName: 'constructor',
  50. paramName: 'pattern',
  51. });
  52. }
  53. const match = ({url}) => {
  54. const result = regExp.exec(url.href);
  55. // Return null immediately if there's no match.
  56. if (!result) {
  57. return null;
  58. }
  59. // Require that the match start at the first character in the URL string
  60. // if it's a cross-origin request.
  61. // See https://github.com/GoogleChrome/workbox/issues/281 for the context
  62. // behind this behavior.
  63. if ((url.origin !== location.origin) && (result.index !== 0)) {
  64. if (process.env.NODE_ENV !== 'production') {
  65. logger.debug(
  66. `The regular expression '${regExp}' only partially matched ` +
  67. `against the cross-origin URL '${url}'. RegExpRoute's will only ` +
  68. `handle cross-origin requests if they match the entire URL.`
  69. );
  70. }
  71. return null;
  72. }
  73. // If the route matches, but there aren't any capture groups defined, then
  74. // this will return [], which is truthy and therefore sufficient to
  75. // indicate a match.
  76. // If there are capture groups, then it will return their values.
  77. return result.slice(1);
  78. };
  79. super(match, handler, method);
  80. }
  81. }
  82. export {RegExpRoute};