diff --git a/src/index.ts b/src/index.ts index a667c297..258ea862 100644 --- a/src/index.ts +++ b/src/index.ts @@ -78,8 +78,13 @@ export function useQueryState( // Don't leave value-less keys hanging query.delete(key) } + + // Remove fragment and query from asPath + // router.pathname includes dynamic route keys, rather than the route itself, + // e.g. /views/[view] rather than /views/my-view + const [asPath] = router.asPath.split(/\?|#/, 1) updateUrl?.call(router, { - pathname: router.pathname, + pathname: asPath, hash: window.location.hash, search: query.toString() })