pbeshai / use-query-params

React Hook for managing state in URL query parameters with easy serialization.

Home Page:https://pbeshai.github.io/use-query-params

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`updateLocation` can script crash when creating a `new URL()`

jmcpeak opened this issue · comments

Please see the diff here, at line 50:

4d7a089?diff=split#diff-2b69e8addd21fff5dab3f789e51a2582b63e95e28e393f7319e887d29de174e7

Previously href was calculated this way:
const href = parseUrl(location.href || '').url + search;

Now it is this:

let href: string;
  if (location.href) {
    const url = new URL(location.href);
    href = `${url.origin}${url.pathname}${search}`;
  } else {
    href = search;
  }

If you call the updateLocation with no href, it will set href to be the search value.
Now if you call updateLocation again, reusing the same location object that was returned, href will be the search value.
This causes an exception at this line:
const url = new URL(location.href);
because URL requires a valid URL - "If the given base URL or the resulting URL are not valid URLs, the JavaScript TypeError exception is thrown." see https://developer.mozilla.org/en-US/docs/Web/API/URL/URL

PR created - #245