sindresorhus/normalize-url

`removeQueryParameters` fails in Firefox content scripts: Error `URLSearchParams.keys()` is not a method

Closed this issue · 4 comments

The option removeQueryParameters of normalizeUrl with a list of RegExps raises an error of "Error: o.searchParams.keys() is not iterable" only in Firefox content scripts.

I compile code from TypeScript, the code snipet with usage of normalizeUrl is following:

normalizeUrl('https://github.com/sindresorhus/normalize-url/issues/', {
   forceHttps: true,
   normalizeProtocol: true,
   removeTrailingSlash: true,
   removeQueryParameters: [/^utm_\w+/i, /^itm_\w+/i],
   sortQueryParameters: true,
   stripAuthentication: true,
   stripHash: true,
   stripProtocol: false,
   stripTextFragment: true,
   stripWWW: true,
})
  • Version: 7.0.3

  • tsconfig.compilerOptions:

    • lib: ["ES2020","dom","dom.iterable"]
    • target: es6
    • module: ES2020
    • moduleResolution: node
    • allowJs: true

    Update: if rewrite related code to use urlObject.searchParams.forEach it works just fine.

This has nothing to do specifically with this package. It's either a TS config issue or a Firefox issue.

This has nothing to do specifically with this package. It's either a TS config issue or a Firefox issue

Thanks for the update!

Do you think it doesn't make sense to use .forEach instead? It would be more transferable and even cheaper for URLs with many query params -

  • Complexity O(n), where n is a number query parameters. Same as before.
  • Memory O(k), where k is a number of query parameters to delete. k ≪ n. Now it's O(n).

I can make a PR to fix it. It can be something like this:

  if (Array.isArray(removeQueryParameters)) {
    const keysToDelete: string[] = []
    urlObject.searchParams.forEach((_value: string, key: string) => {
      if (testParameter(key, removeQueryParameters)) {
        keysToDelete.push(key)
      }
    })
    keysToDelete.forEach((key: string) => urlObject.searchParams.delete(key))
  }

What do you think @sindresorhus ?

Do you think it doesn't make sense to use .forEach instead?

I'm not interested in using .forEach. I prefer for-of.

Sure, thanks for the information then!