Skip to content

Add a replace option to useRouteQuery - #11

Merged
greegus merged 2 commits into
masterfrom
feat/use-route-query-replace
Aug 28, 2026
Merged

greegus merged 2 commits into
masterfrom
feat/use-route-query-replace

Conversation

@greegus

@greegus greegus commented Aug 28, 2026

Copy link
Copy Markdown
Owner

Problem

useRouteQuery's setQuery and setQueryParam always call router.push(). When the composable is used to keep UI filters (search terms, pagination, sort order) in the URL, every keystroke or filter change adds a new browser-history entry. The back button then walks back through a sequence of filter states instead of leaving the page, which is not what users expect from filter UI.

Change

Adds an optional replace?: boolean to the composable's options. When set, both setQuery and setQueryParam call router.replace() instead of router.push(), updating the current history entry in place rather than pushing a new one. The default remains push, so this is fully backwards compatible — existing callers see no behavior change.

const { setQueryParam } = useRouteQuery({
  filter: ['search'],
  replace: true,
})

Downstream use case

Writing list filters into the URL (search, category, sort, page) without polluting browser history — each filter change updates the query string but the back button still takes the user out of the filtered list rather than through every intermediate filter state.

Testing

  • Added two tests covering the replace path for both setQuery and setQueryParam.
  • npm run type-check — passes
  • npm run lint — passes
  • oxfmt --check — all files correctly formatted
  • npm test — 736/736 tests passing (46 files)
  • npm run build — builds successfully

setQuery and setQueryParam always called router.push(), so every
filter update added a browser-history entry and the back button
walked through filter states instead of leaving the page. Passing
replace: true now routes both setters through router.replace()
instead, keeping the default push behavior for existing callers.
@greegus
greegus merged commit b41438f into master Aug 28, 2026
1 check passed
@greegus
greegus deleted the feat/use-route-query-replace branch August 28, 2026 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant