NavigationRoute.ts 4.3 KB

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