removeIgnoredSearchParams.js 1.0 KB

123456789101112131415161718192021222324252627282930
  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 '../_version.js';
  8. /**
  9. * Removes any URL search parameters that should be ignored.
  10. *
  11. * @param {URL} urlObject The original URL.
  12. * @param {Array<RegExp>} ignoreURLParametersMatching RegExps to test against
  13. * each search parameter name. Matches mean that the search parameter should be
  14. * ignored.
  15. * @return {URL} The URL with any ignored search parameters removed.
  16. *
  17. * @private
  18. * @memberof module:workbox-precaching
  19. */
  20. export function removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching = []) {
  21. // Convert the iterable into an array at the start of the loop to make sure
  22. // deletion doesn't mess up iteration.
  23. for (const paramName of [...urlObject.searchParams.keys()]) {
  24. if (ignoreURLParametersMatching.some((regExp) => regExp.test(paramName))) {
  25. urlObject.searchParams.delete(paramName);
  26. }
  27. }
  28. return urlObject;
  29. }