Skip to content

useUrlSync

Syncs a reactive state object to/from URL query params, bidirectionally and SSR-safe.

On call (sync): reads the current URL and pre-populates state before any child mounts — no flash of defaults on page load or after a hard refresh.

Reactively: watches state and updates the URL on every change via router.replace (no new history entries).

Usage

ts
const filters = ref({
  search: '',
  sort: 'newest',
  price: { min: 0, max: 500 },
})

useUrlSync(filters)

Pass an explicit key list to avoid accidentally syncing internal or non-serialisable state:

ts
const state = ref({
  category: '',
  sort: 'newest',
  _sessionToken: 'abc123', // never touches the URL
})

useUrlSync(state, ['category', 'sort'])

Encoding

ValueURL
{ price: { min: 10, max: 500 } }price.min=10&price.max=500
{ tags: ['vue', 'ts'] }tags[0]=vue&tags[1]=ts
{ items: [{ name: 'Alice' }] }items[0].name=Alice
empty stringremoved from URL
File / Blobsilently skipped

Parameters

ParameterTypeDescription
stateRef<Record<string, unknown>>Reactive object to sync
keysstring[]Top-level keys to sync. Omit to sync all keys in state / URL

Returns void.