NavigationRoute.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * NavigationRoute makes it easy to create a
  13. * [Route]{@link module:workbox-routing.Route} that matches for browser
  14. * [navigation requests]{@link https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests}.
  15. *
  16. * It will only match incoming Requests whose
  17. * [`mode`]{@link https://fetch.spec.whatwg.org/#concept-request-mode}
  18. * is set to `navigate`.
  19. *
  20. * You can optionally only apply this route to a subset of navigation requests
  21. * by using one or both of the `denylist` and `allowlist` parameters.
  22. *
  23. * @memberof module:workbox-routing
  24. * @extends module:workbox-routing.Route
  25. */
  26. class NavigationRoute extends Route {
  27. /**
  28. * If both `denylist` and `allowlist` are provided, the `denylist` will
  29. * take precedence and the request will not match this route.
  30. *
  31. * The regular expressions in `allowlist` and `denylist`
  32. * are matched against the concatenated
  33. * [`pathname`]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/pathname}
  34. * and [`search`]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search}
  35. * portions of the requested URL.
  36. *
  37. * @param {module:workbox-routing~handlerCallback} handler A callback
  38. * function that returns a Promise resulting in a Response.
  39. * @param {Object} options
  40. * @param {Array<RegExp>} [options.denylist] If any of these patterns match,
  41. * the route will not handle the request (even if a allowlist RegExp matches).
  42. * @param {Array<RegExp>} [options.allowlist=[/./]] If any of these patterns
  43. * match the URL's pathname and search parameter, the route will handle the
  44. * request (assuming the denylist doesn't match).
  45. */
  46. constructor(handler, { allowlist = [/./], denylist = [] } = {}) {
  47. if (process.env.NODE_ENV !== 'production') {
  48. assert.isArrayOfClass(allowlist, RegExp, {
  49. moduleName: 'workbox-routing',
  50. className: 'NavigationRoute',
  51. funcName: 'constructor',
  52. paramName: 'options.allowlist',
  53. });
  54. assert.isArrayOfClass(denylist, RegExp, {
  55. moduleName: 'workbox-routing',
  56. className: 'NavigationRoute',
  57. funcName: 'constructor',
  58. paramName: 'options.denylist',
  59. });
  60. }
  61. super((options) => this._match(options), handler);
  62. this._allowlist = allowlist;
  63. this._denylist = denylist;
  64. }
  65. /**
  66. * Routes match handler.
  67. *
  68. * @param {Object} options
  69. * @param {URL} options.url
  70. * @param {Request} options.request
  71. * @return {boolean}
  72. *
  73. * @private
  74. */
  75. _match({ url, request }) {
  76. if (request && request.mode !== 'navigate') {
  77. return false;
  78. }
  79. const pathnameAndSearch = url.pathname + url.search;
  80. for (const regExp of this._denylist) {
  81. if (regExp.test(pathnameAndSearch)) {
  82. if (process.env.NODE_ENV !== 'production') {
  83. logger.log(`The navigation route ${pathnameAndSearch} is not ` +
  84. `being used, since the URL matches this denylist pattern: ` +
  85. `${regExp}`);
  86. }
  87. return false;
  88. }
  89. }
  90. if (this._allowlist.some((regExp) => regExp.test(pathnameAndSearch))) {
  91. if (process.env.NODE_ENV !== 'production') {
  92. logger.debug(`The navigation route ${pathnameAndSearch} ` +
  93. `is being used.`);
  94. }
  95. return true;
  96. }
  97. if (process.env.NODE_ENV !== 'production') {
  98. logger.log(`The navigation route ${pathnameAndSearch} is not ` +
  99. `being used, since the URL being navigated to doesn't ` +
  100. `match the allowlist.`);
  101. }
  102. return false;
  103. }
  104. }
  105. export { NavigationRoute };