NavigationRoute.mjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.mjs';
  8. import {logger} from 'workbox-core/_private/logger.mjs';
  9. import {Route} from './Route.mjs';
  10. import './_version.mjs';
  11. /**
  12. * NavigationRoute makes it easy to create a [Route]{@link
  13. * 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 `blacklist` and `whitelist` parameters.
  22. *
  23. * @memberof workbox.routing
  24. * @extends workbox.routing.Route
  25. */
  26. class NavigationRoute extends Route {
  27. /**
  28. * If both `blacklist` and `whiltelist` are provided, the `blacklist` will
  29. * take precedence and the request will not match this route.
  30. *
  31. * The regular expressions in `whitelist` and `blacklist`
  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 {workbox.routing.Route~handlerCallback} handler A callback
  38. * function that returns a Promise resulting in a Response.
  39. * @param {Object} options
  40. * @param {Array<RegExp>} [options.blacklist] If any of these patterns match,
  41. * the route will not handle the request (even if a whitelist RegExp matches).
  42. * @param {Array<RegExp>} [options.whitelist=[/./]] If any of these patterns
  43. * match the URL's pathname and search parameter, the route will handle the
  44. * request (assuming the blacklist doesn't match).
  45. */
  46. constructor(handler, {whitelist = [/./], blacklist = []} = {}) {
  47. if (process.env.NODE_ENV !== 'production') {
  48. assert.isArrayOfClass(whitelist, RegExp, {
  49. moduleName: 'workbox-routing',
  50. className: 'NavigationRoute',
  51. funcName: 'constructor',
  52. paramName: 'options.whitelist',
  53. });
  54. assert.isArrayOfClass(blacklist, RegExp, {
  55. moduleName: 'workbox-routing',
  56. className: 'NavigationRoute',
  57. funcName: 'constructor',
  58. paramName: 'options.blacklist',
  59. });
  60. }
  61. super((options) => this._match(options), handler);
  62. this._whitelist = whitelist;
  63. this._blacklist = blacklist;
  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.mode !== 'navigate') {
  77. return false;
  78. }
  79. const pathnameAndSearch = url.pathname + url.search;
  80. for (const regExp of this._blacklist) {
  81. if (regExp.test(pathnameAndSearch)) {
  82. if (process.env.NODE_ENV !== 'production') {
  83. logger.log(`The navigation route is not being used, since the ` +
  84. `URL matches this blacklist pattern: ${regExp}`);
  85. }
  86. return false;
  87. }
  88. }
  89. if (this._whitelist.some((regExp) => regExp.test(pathnameAndSearch))) {
  90. if (process.env.NODE_ENV !== 'production') {
  91. logger.debug(`The navigation route is being used.`);
  92. }
  93. return true;
  94. }
  95. if (process.env.NODE_ENV !== 'production') {
  96. logger.log(`The navigation route is not being used, since the URL ` +
  97. `being navigated to doesn't match the whitelist.`);
  98. }
  99. return false;
  100. }
  101. }
  102. export {NavigationRoute};