kit icon indicating copy to clipboard operation
kit copied to clipboard

Cannot fully clear query params with pushState

Open andykais opened this issue 6 months ago • 1 comments

Describe the problem

I am using query params on a search page to store things like ?tags=foo,bar and when the user inputs an "empty" search (e.g. no tags filter), I want to clear away all the query params on the page. The problem is, pushState treats pushState('', {}) as a special call that does not update the route. The best I can do is pushState('?', {}) which leaves an ugly question mark in my url like so: /browse?.

Describe the proposed solution

What I would like is a way to fully clear away query params and the lingering ? character. I'm thinking this can either be the default behavior of pushState('?', {}) or it could be a special flag added like pushState('', {clearQueryParams: true})

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

andykais avatar May 25 '25 23:05 andykais

The vanilla pushState works by resolving the URL you pass it against the current URL, which is why calling with '' gives you the same URL.

Does calling pushState with location.pathname not work?

Conduitry avatar May 26 '25 00:05 Conduitry