VERY IMPORTANT: This utility is currently in an experimental stage. This means that breaking changes will happen in minor AND patch releases. Use at your own risk. If you choose to rely on this in production in an experimental stage, please lock your version to a patch-level version to avoid unexpected breakages.
persistQueryClient
is a utility for persisting the state of your queryClient and its caches for later use. Different persistors can be used to store your client and cache to many different storage layers.
This utility comes packaged with svelte-query
and is available under the @sveltestack/svelte-query
import.
Import the persistQueryClient
function, and pass it your QueryClient
instance (with a cacheTime
set), and a Persistor interface (there are multiple persistor types you can use):
import { persistQueryClient } from '`@sveltestack/svelte-query`'import { createLocalStoragePersistor } from '`@sveltestack/svelte-query`'const queryClient = new QueryClient({defaultOptions: {queries: {cacheTime: 1000 * 60 * 60 * 24, // 24 hours},},})const localStoragePersistor = createLocalStoragePersistor()persistQueryClient({queryClientpersistor: localStoragePersistor,})
IMPORTANT - for persist to work properly, you need to pass QueryClient
a cacheTime
value to override the default during hydration (as shown above).
If it is not set when creating the QueryClient
instance, it will default to 300000
(5 minutes) for hydration, and the stored cache will be discarded after 5 minutes of inactivity. This is the default garbage collection behavior.
It should be set as the same value or higher than persistQueryClient's maxAge
option. E.g. if maxAge
is 24 hours (the default) then cacheTime
should be 24 hours or higher. If lower than maxAge
, garbage collection will kick in and discard the stored cache earlier than expected.
You can also pass it Infinity
to disable garbage collection behavior entirely.
As you use your application:
When you reload/bootstrap your app:
maxAge
(which by default is 24 hours), it will be discarded. This can be customized as you see fit.Sometimes you may make changes to your application or data that immediately invalidate any and all cached data. If and when this happens, you can pass a buster
string option to persistQueryClient
, and if the cache that is found does not also have that buster string, it will be discarded.
persistQueryClient({ queryClient, persistor, buster: buildHash })
persistQueryClient
Pass this function a QueryClient
instance and a persistor that will persist your cache. Both are required
persistQueryClient({ queryClient, persistor })
Options
An object of options:
interface PersistQueryClientOptions {/** The QueryClient to persist */queryClient: QueryClient/** The Persistor interface for storing and restoring the cache* to/from a persisted location */persistor: Persistor/** The max-allowed age of the cache.* If a persisted cache is found that is older than this* time, it will be discarded */maxAge?: number/** A unique string that can be used to forcefully* invalidate existing caches if they do not share the same buster string */buster?: string}
The default options are:
{maxAge = 1000 * 60 * 60 * 24, // 24 hoursbuster = '',}
Persistors have the following interface:
export interface Persistor {persistClient(persistClient: PersistedClient): Promisable<void>restoreClient(): Promisable<PersistedClient | undefined>removeClient(): Promisable<void>}
Persisted Client entries have the following interface:
export interface PersistedClient {timestamp: numberbuster: stringcacheState: any}
Satisfy all of these interfaces and you've got yourself a persistor!
The latest TanStack news, articles, and resources, sent to your inbox.