generateURLVariations.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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.js';
  8. import '../_version.js';
  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, { ignoreURLParametersMatching, directoryIndex, cleanURLs, urlManipulation, } = {}) {
  20. const urlObject = new URL(url, location.href);
  21. urlObject.hash = '';
  22. yield urlObject.href;
  23. const urlWithoutIgnoredParams = removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching);
  24. yield urlWithoutIgnoredParams.href;
  25. if (directoryIndex && urlWithoutIgnoredParams.pathname.endsWith('/')) {
  26. const directoryURL = new URL(urlWithoutIgnoredParams.href);
  27. directoryURL.pathname += directoryIndex;
  28. yield directoryURL.href;
  29. }
  30. if (cleanURLs) {
  31. const cleanURL = new URL(urlWithoutIgnoredParams.href);
  32. cleanURL.pathname += '.html';
  33. yield cleanURL.href;
  34. }
  35. if (urlManipulation) {
  36. const additionalURLs = urlManipulation({ url: urlObject });
  37. for (const urlToAttempt of additionalURLs) {
  38. yield urlToAttempt.href;
  39. }
  40. }
  41. }