Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,14 @@ const { queryParams } = useRouteQuery({
})
```

```typescript
// Update the current history entry instead of pushing a new one, e.g. for filters
const { setQueryParam } = useRouteQuery({
filter: ['search'],
replace: true
})
```

### useSubmitAction

**File:** `src/composables/useSubmitAction.ts`
Expand Down
8 changes: 8 additions & 0 deletions docs/composables/use-route-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ const { queryParams } = useRouteQuery({
immediate: true,
})
```

```typescript
// Update the current history entry instead of pushing a new one, e.g. for filters
const { setQueryParam } = useRouteQuery({
filter: ['search'],
replace: true,
})
```
26 changes: 26 additions & 0 deletions src/composables/__tests__/useRouteQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,30 @@ describe('useRouteQuery', () => {

expect(onChange).not.toHaveBeenCalled()
})

it('uses router.replace instead of router.push when the replace option is set', async () => {
const { result, router } = await withRouteQuery(() => useRouteQuery({ replace: true }), { search: 'hello' })
const pushSpy = vi.spyOn(router, 'push')
const replaceSpy = vi.spyOn(router, 'replace')

result.setQueryParam('search', 'world')
await flushPromises()

expect(replaceSpy).toHaveBeenCalledWith({ query: { search: 'world' } })
expect(pushSpy).not.toHaveBeenCalled()
expect(router.currentRoute.value.query).toEqual({ search: 'world' })
})

it('uses router.replace on setQuery when the replace option is set', async () => {
const { result, router } = await withRouteQuery(() => useRouteQuery({ replace: true }), { search: 'hello' })
const pushSpy = vi.spyOn(router, 'push')
const replaceSpy = vi.spyOn(router, 'replace')

result.setQuery({ category: 'books' })
await flushPromises()

expect(replaceSpy).toHaveBeenCalledWith({ query: { category: 'books' } })
expect(pushSpy).not.toHaveBeenCalled()
expect(router.currentRoute.value.query).toEqual({ category: 'books' })
})
})
16 changes: 14 additions & 2 deletions src/composables/useRouteQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ import { useRoute, useRouter } from 'vue-router'
* onChange: (params) => fetchData(params),
* immediate: true
* })
*
* @example
* // Update the current history entry instead of pushing a new one, e.g. for filters
* const { setQueryParam } = useRouteQuery({
* filter: ['search'],
* replace: true
* })
*/

const valueIsNotEmpty = (value: any) => {
Expand All @@ -47,6 +54,8 @@ export function useRouteQuery<QueryParams extends Record<string, unknown> = Reco
serialize?: Record<keyof QueryParams, (value: QueryParams[keyof QueryParams]) => string>
immediate?: boolean
defaults?: Partial<QueryParams>
/** Use `router.replace()` instead of `router.push()`, so updates don't add browser-history entries. */
replace?: boolean
}): {
queryParams: Ref<QueryParams>
setQuery: (params: Partial<QueryParams>) => void
Expand Down Expand Up @@ -90,14 +99,17 @@ export function useRouteQuery<QueryParams extends Record<string, unknown> = Reco
return encodeURIComponent(options.serialize?.[key] ? options.serialize[key](value) : value)
}

const navigate = (location: Parameters<typeof router.push>[0]) =>
options.replace ? router.replace(location) : router.push(location)

const setQuery = (params: Partial<QueryParams>) => {
const serializedParams = Object.fromEntries(
Object.entries(params)
.filter(([_key, value]) => valueIsNotEmpty(value))
.map(([key, value]) => [key, serializeValue(key, value)]),
) as Record<string, string>

return router.push({
return navigate({
query: serializedParams,
})
}
Expand All @@ -109,7 +121,7 @@ export function useRouteQuery<QueryParams extends Record<string, unknown> = Reco

newQueryParams[key as string] = valueIsNotEmpty(value) ? serializeValue(key, value) : (undefined as any)

return router.push({ query: newQueryParams })
return navigate({ query: newQueryParams })
}

// Watched through a serialized snapshot, because `queryParams` is a computed that rebuilds a
Expand Down