generateURLVariations.mjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Copyright 2019 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 {removeIgnoredSearchParams} from './removeIgnoredSearchParams.mjs';
  8. import '../_version.mjs';
  9. /**
  10. * Generator function that yields possible variations on the original URL to
  11. * check, one at a time.
  12. *
  13. * @param {string} url
  14. * @param {Object} options
  15. *
  16. * @private
  17. * @memberof module:workbox-precaching
  18. */
  19. export function* generateURLVariations(url, {
  20. ignoreURLParametersMatching,
  21. directoryIndex,
  22. cleanURLs,
  23. urlManipulation,
  24. } = {}) {
  25. const urlObject = new URL(url, location);
  26. urlObject.hash = '';
  27. yield urlObject.href;
  28. const urlWithoutIgnoredParams = removeIgnoredSearchParams(
  29. urlObject, ignoreURLParametersMatching);
  30. yield urlWithoutIgnoredParams.href;
  31. if (directoryIndex && urlWithoutIgnoredParams.pathname.endsWith('/')) {
  32. const directoryURL = new URL(urlWithoutIgnoredParams);
  33. directoryURL.pathname += directoryIndex;
  34. yield directoryURL.href;
  35. }
  36. if (cleanURLs) {
  37. const cleanURL = new URL(urlWithoutIgnoredParams);
  38. cleanURL.pathname += '.html';
  39. yield cleanURL.href;
  40. }
  41. if (urlManipulation) {
  42. const additionalURLs = urlManipulation({url: urlObject});
  43. for (const urlToAttempt of additionalURLs) {
  44. yield urlToAttempt.href;
  45. }
  46. }
  47. }