No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

NavigationRoute.mjs 4.2 KiB

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