diff --git a/src/createAsyncStoragePersister/index.ts b/src/createAsyncStoragePersister/index.ts index 57122af0be..b033c22858 100644 --- a/src/createAsyncStoragePersister/index.ts +++ b/src/createAsyncStoragePersister/index.ts @@ -1,13 +1,13 @@ import { PersistedClient, Persister } from '../persistQueryClient' import { asyncThrottle } from './asyncThrottle' -interface AsyncStorage { +export interface AsyncStorage { getItem: (key: string) => Promise setItem: (key: string, value: string) => Promise removeItem: (key: string) => Promise } -interface CreateAsyncStoragePersisterOptions { +export interface CreateAsyncStoragePersisterOptions { /** The storage client used for setting an retrieving items from cache */ storage: AsyncStorage /** The key to use when storing the cache */ diff --git a/src/createWebStoragePersister/index.ts b/src/createWebStoragePersister/index.ts index a6493b707f..9c663f8bc4 100644 --- a/src/createWebStoragePersister/index.ts +++ b/src/createWebStoragePersister/index.ts @@ -2,8 +2,10 @@ import { noop } from '../core/utils' import { PersistedClient, Persister } from '../persistQueryClient' interface CreateWebStoragePersisterOptions { - /** The storage client used for setting an retrieving items from cache */ - storage: Storage + /** The storage client used for setting an retrieving items from cache. + * For SSR pass in `undefined`. + */ + storage?: Storage /** The key to use when storing the cache */ key?: string /** To avoid spamming, @@ -31,7 +33,7 @@ export function createWebStoragePersister({ //try to save data to storage function trySave(persistedClient: PersistedClient) { try { - storage.setItem(key, serialize(persistedClient)) + storage?.setItem(key, serialize(persistedClient)) } catch { return false } diff --git a/src/persistQueryClient/persist.ts b/src/persistQueryClient/persist.ts index 13af8d6253..c5c5790907 100644 --- a/src/persistQueryClient/persist.ts +++ b/src/persistQueryClient/persist.ts @@ -65,34 +65,32 @@ export async function persistQueryClientRestore({ buster = '', hydrateOptions, }: PersistedQueryClientRestoreOptions) { - if (typeof window !== 'undefined') { - try { - const persistedClient = await persister.restoreClient() - - if (persistedClient) { - if (persistedClient.timestamp) { - const expired = Date.now() - persistedClient.timestamp > maxAge - const busted = persistedClient.buster !== buster - if (expired || busted) { - persister.removeClient() - } else { - hydrate(queryClient, persistedClient.clientState, hydrateOptions) - } - } else { + try { + const persistedClient = await persister.restoreClient() + + if (persistedClient) { + if (persistedClient.timestamp) { + const expired = Date.now() - persistedClient.timestamp > maxAge + const busted = persistedClient.buster !== buster + if (expired || busted) { persister.removeClient() + } else { + hydrate(queryClient, persistedClient.clientState, hydrateOptions) } + } else { + persister.removeClient() } - } catch (err) { - if (process.env.NODE_ENV !== 'production') { - queryClient.getLogger().error(err) - queryClient - .getLogger() - .warn( - 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.' - ) - } - persister.removeClient() } + } catch (err) { + if (process.env.NODE_ENV !== 'production') { + queryClient.getLogger().error(err) + queryClient + .getLogger() + .warn( + 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.' + ) + } + persister.removeClient() } } @@ -107,15 +105,13 @@ export async function persistQueryClientSave({ buster = '', dehydrateOptions, }: PersistedQueryClientSaveOptions) { - if (typeof window !== 'undefined') { - const persistClient: PersistedClient = { - buster, - timestamp: Date.now(), - clientState: dehydrate(queryClient, dehydrateOptions), - } - - await persister.persistClient(persistClient) + const persistClient: PersistedClient = { + buster, + timestamp: Date.now(), + clientState: dehydrate(queryClient, dehydrateOptions), } + + await persister.persistClient(persistClient) } /** @@ -156,17 +152,13 @@ export function persistQueryClient( persistQueryClientUnsubscribe?.() } - let restorePromise = Promise.resolve() - - if (typeof window !== 'undefined') { - // Attempt restore - restorePromise = persistQueryClientRestore(props).then(() => { - if (!hasUnsubscribed) { - // Subscribe to changes in the query cache to trigger the save - persistQueryClientUnsubscribe = persistQueryClientSubscribe(props) - } - }) - } + // Attempt restore + const restorePromise = persistQueryClientRestore(props).then(() => { + if (!hasUnsubscribed) { + // Subscribe to changes in the query cache to trigger the save + persistQueryClientUnsubscribe = persistQueryClientSubscribe(props) + } + }) return [unsubscribe, restorePromise] }