From 29968f4a3c9427be520d2af6403e7280822b345f Mon Sep 17 00:00:00 2001 From: Thomas Paul Mann Date: Fri, 18 Mar 2022 20:36:30 +0100 Subject: [PATCH 01/11] Export interfaces to create type-safe `AsyncStorage` --- src/createAsyncStoragePersister/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/createAsyncStoragePersister/index.ts b/src/createAsyncStoragePersister/index.ts index 57122af0be..5c9950282e 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 CreateAsyncStoragePersistorOptions { /** The storage client used for setting an retrieving items from cache */ storage: AsyncStorage /** The key to use when storing the cache */ From e9af44b52f26e7fea77d08b2e1493d87e9a779ca Mon Sep 17 00:00:00 2001 From: Thomas Paul Mann Date: Fri, 18 Mar 2022 20:37:26 +0100 Subject: [PATCH 02/11] Remove checks for window in `persistQueryClient` --- src/persistQueryClient/index.ts | 68 +++++++++++++++------------------ 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/src/persistQueryClient/index.ts b/src/persistQueryClient/index.ts index 407e58f4ed..54f99c876d 100644 --- a/src/persistQueryClient/index.ts +++ b/src/persistQueryClient/index.ts @@ -65,34 +65,32 @@ export async function persistQueryClientRestore({ buster = '', hydrateOptions, }: PersistedQueryClientRestoreOptions) { - if (typeof window !== 'undefined') { - try { - const persistedClient = await persister.restoreClient() + 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 { + 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) } /** @@ -135,11 +131,9 @@ export function persistQueryClientSubscribe( * (Retained for backwards compatibility) */ export async function persistQueryClient(props: PersistQueryClientOptions) { - if (typeof window !== 'undefined') { - // Attempt restore - await persistQueryClientRestore(props) + // Attempt restore + await persistQueryClientRestore(props) - // Subscribe to changes in the query cache to trigger the save - return persistQueryClientSubscribe(props) - } + // Subscribe to changes in the query cache to trigger the save + return persistQueryClientSubscribe(props) } From 2befefef4de23f168762ddf475247c24da649d9d Mon Sep 17 00:00:00 2001 From: Thomas Paul Mann Date: Fri, 18 Mar 2022 20:42:28 +0100 Subject: [PATCH 03/11] Fix typo --- src/createAsyncStoragePersister/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/createAsyncStoragePersister/index.ts b/src/createAsyncStoragePersister/index.ts index 5c9950282e..b033c22858 100644 --- a/src/createAsyncStoragePersister/index.ts +++ b/src/createAsyncStoragePersister/index.ts @@ -7,7 +7,7 @@ export interface AsyncStorage { removeItem: (key: string) => Promise } -export interface CreateAsyncStoragePersistorOptions { +export interface CreateAsyncStoragePersisterOptions { /** The storage client used for setting an retrieving items from cache */ storage: AsyncStorage /** The key to use when storing the cache */ From 80ceaa66ad23a5a51f286e396cc073290284016d Mon Sep 17 00:00:00 2001 From: Thomas Paul Mann Date: Sat, 2 Apr 2022 15:12:19 +0200 Subject: [PATCH 04/11] Update src/reactjs/useIsFetching.ts --- src/reactjs/useIsFetching.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/reactjs/useIsFetching.ts b/src/reactjs/useIsFetching.ts index 9aadcc5436..2fe9554cb2 100644 --- a/src/reactjs/useIsFetching.ts +++ b/src/reactjs/useIsFetching.ts @@ -4,7 +4,6 @@ import { useSyncExternalStore } from 'use-sync-external-store/shim' import { ContextOptions } from './types' import { QueryKey, notifyManager } from '../core' import { parseFilterArgs, QueryFilters } from '../core/utils' -import { QueryClient } from '../core' import { useQueryClient } from './QueryClientProvider' interface Options extends ContextOptions {} From fcf0f85308edd7096c69cd02a8008b73cd27cbf2 Mon Sep 17 00:00:00 2001 From: Thomas Paul Mann Date: Sat, 2 Apr 2022 15:12:38 +0200 Subject: [PATCH 05/11] Update src/reactjs/tests/useIsFetching.test.tsx --- src/reactjs/tests/useIsFetching.test.tsx | 25 ------------------------ 1 file changed, 25 deletions(-) diff --git a/src/reactjs/tests/useIsFetching.test.tsx b/src/reactjs/tests/useIsFetching.test.tsx index 52d87847af..78fdfcfae5 100644 --- a/src/reactjs/tests/useIsFetching.test.tsx +++ b/src/reactjs/tests/useIsFetching.test.tsx @@ -262,29 +262,4 @@ describe('useIsFetching', () => { await rendered.findByText('isFetching: 1') await rendered.findByText('isFetching: 0') }) - - it('should show the correct fetching state when mounted after a query', async () => { - const queryClient = new QueryClient() - const key = queryKey() - - function Page() { - useQuery(key, async () => { - await sleep(10) - return 'test' - }) - - const isFetching = useIsFetching() - - return ( -
-
isFetching: {isFetching}
-
- ) - } - - const rendered = renderWithClient(queryClient, ) - - await rendered.findByText('isFetching: 1') - await rendered.findByText('isFetching: 0') - }) }) From 614b81151453f00ab865f5c0e6508dde56199bdd Mon Sep 17 00:00:00 2001 From: Thomas Paul Mann Date: Sat, 2 Apr 2022 15:12:52 +0200 Subject: [PATCH 06/11] Update src/devtools/utils.ts --- src/devtools/utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/devtools/utils.ts b/src/devtools/utils.ts index 06a506225b..903f71e237 100644 --- a/src/devtools/utils.ts +++ b/src/devtools/utils.ts @@ -121,6 +121,5 @@ export function useIsMounted() { export const displayValue = (value: unknown) => { const name = Object.getOwnPropertyNames(Object(value)) const newValue = typeof value === 'bigint' ? `${value.toString()}n` : value - return JSON.stringify(newValue, name) } From 28938b2f9e0e84d53f3594a3da89139302c4f607 Mon Sep 17 00:00:00 2001 From: Thomas Paul Mann Date: Sat, 2 Apr 2022 15:13:07 +0200 Subject: [PATCH 07/11] Update src/persistQueryClient/index.ts --- src/persistQueryClient/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/persistQueryClient/index.ts b/src/persistQueryClient/index.ts index f6185a3c72..0ee1179f5f 100644 --- a/src/persistQueryClient/index.ts +++ b/src/persistQueryClient/index.ts @@ -1,2 +1,2 @@ export * from './persist' -export * from './PersistQueryClientProvider' \ No newline at end of file +export * from './PersistQueryClientProvider' From e369b0f487e9a4b00c8e2aeba820a71135e18261 Mon Sep 17 00:00:00 2001 From: Thomas Paul Mann Date: Sat, 2 Apr 2022 15:15:54 +0200 Subject: [PATCH 08/11] Remove checks for `window` --- src/persistQueryClient/persist.ts | 77 ++++++++++++++----------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/src/persistQueryClient/persist.ts b/src/persistQueryClient/persist.ts index 13af8d6253..e327a7db00 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,14 @@ export function persistQueryClient( persistQueryClientUnsubscribe?.() } + // Attempt restore 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) - } - }) - } + restorePromise = persistQueryClientRestore(props).then(() => { + if (!hasUnsubscribed) { + // Subscribe to changes in the query cache to trigger the save + persistQueryClientUnsubscribe = persistQueryClientSubscribe(props) + } + }) return [unsubscribe, restorePromise] } From 70341d60c1809666bc8a8f5b4381c025563c1920 Mon Sep 17 00:00:00 2001 From: Thomas Paul Mann Date: Sat, 2 Apr 2022 15:18:00 +0200 Subject: [PATCH 09/11] Remove unnecessary changes --- src/devtools/utils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/devtools/utils.ts b/src/devtools/utils.ts index 903f71e237..56daf9201c 100644 --- a/src/devtools/utils.ts +++ b/src/devtools/utils.ts @@ -121,5 +121,6 @@ export function useIsMounted() { export const displayValue = (value: unknown) => { const name = Object.getOwnPropertyNames(Object(value)) const newValue = typeof value === 'bigint' ? `${value.toString()}n` : value + return JSON.stringify(newValue, name) } From e1350f979236eebcc24c11dccae70c3ab0b4fab3 Mon Sep 17 00:00:00 2001 From: Thomas Paul Mann Date: Fri, 8 Apr 2022 18:07:54 +0200 Subject: [PATCH 10/11] Remove unnecessary `let` --- src/persistQueryClient/persist.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/persistQueryClient/persist.ts b/src/persistQueryClient/persist.ts index e327a7db00..c5c5790907 100644 --- a/src/persistQueryClient/persist.ts +++ b/src/persistQueryClient/persist.ts @@ -153,8 +153,7 @@ export function persistQueryClient( } // Attempt restore - let restorePromise = Promise.resolve() - restorePromise = persistQueryClientRestore(props).then(() => { + const restorePromise = persistQueryClientRestore(props).then(() => { if (!hasUnsubscribed) { // Subscribe to changes in the query cache to trigger the save persistQueryClientUnsubscribe = persistQueryClientSubscribe(props) From 97aa02587ff08a8a243221a59be72768544826f6 Mon Sep 17 00:00:00 2001 From: Thomas Paul Mann Date: Tue, 26 Apr 2022 10:21:39 +0100 Subject: [PATCH 11/11] Make `Storage` optional for `createWebStoragePersister` --- src/createWebStoragePersister/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 }