Skip to content

withStorage @vuqs/core/modules ​

Mirrors explicit query values to storage and can restore a snapshot into an empty state.

Usage ​

ts
import { codecs, useQueryStates } from '@vuqs/core'
import { createWebStorage, withStorage } from '@vuqs/core/modules'

const query = useQueryStates({ q: codecs.string })
  .use(withStorage({
    key: 'catalog:filters',
    storage: createWebStorage(() => window.localStorage),
  }))

await query.storage.ready
query.storage.status.value

Type ​

ts
import type { QueryModuleFactory } from '@vuqs/core'

declare const withStorage: QueryModuleFactory<
  'vuqs:storage',
  typeof import('@vuqs/core/modules').withStorage.definition
>

QueryModuleFactory is exported from @vuqs/core. Its second type parameter preserves the module's setup definitions. See Factory call forms for adaptive, schema, param, and path calls.

Parameters ​

OptionTypeDefaultBehavior
keystringRequiredStorage key for the snapshot.
storageQueryStorageRequiredSynchronous or asynchronous storage implementation.
restoreStorageRestorePolicy'if-empty'Restore only into an empty state, or mirror without loading with 'never'.
versionstringAbsentRejects snapshots with a different application version.

Invalid options or a malformed storage contract throw synchronously during .use().

Return value ​

A module contributing the same StorageApi on either facade. Controls are available at query.storage.

ControlTypeBehavior
statusComputedRef<StorageStatus>Latest lifecycle outcome.
errorShallowRef<unknown | undefined>Latest operational failure; cleared after a successful write.
readyPromise<void>Settles after initialization reaches a stable mirror.
flush() => Promise<void>Waits for writes requested through the point of the call, including initialization.

ready and flush() resolve after operational failures. Inspect status.value and error.value for the outcome; neither promise is an error channel.

Restore and lifecycle ​

A non-empty URL or a write intent during loading wins over storage. A valid stored state restores with one whole-state write only when the URL remains empty. Explicit values equal to defaults are preserved; resolved defaults are not persisted. An empty mirror removes its storage key.

On the server, storage is not read and ready resolves. Restoration starts after mount in a component, or on a microtask outside one. Scope disposal stops observation and prevents late results from restoring or updating module state.

StorageOptions ​

ts
interface StorageOptions {
  key: string
  storage: QueryStorage
  restore?: StorageRestorePolicy
  version?: string
}

StorageRestorePolicy ​

ts
type StorageRestorePolicy = 'if-empty' | 'never'

StorageApi ​

ts
interface StorageApi {
  storage: StorageControls
}

StorageControls ​

ts
interface StorageControls {
  status: ComputedRef<StorageStatus>
  error: ShallowRef<unknown | undefined>
  ready: Promise<void>
  flush: () => Promise<void>
}

StorageStatus ​

ts
type StorageStatus = 'restoring' | 'ready' | 'error'

QueryStorage ​

ts
interface QueryStorage {
  load: (key: string) => Awaitable<StoredQuerySnapshot | undefined>
  save: (key: string, snapshot: StoredQuerySnapshot) => Awaitable<void>
  remove: (key: string) => Awaitable<void>
}

Awaitable ​

ts
type Awaitable<T> = T | PromiseLike<T>

StoredQuerySnapshot ​

ts
interface StoredQuerySnapshot {
  format: 1
  version?: string
  savedAt: number
  query: ParsedQueryRaw
}

Storage.

Released under the MIT License. Inspired by nuqs.