@@ -48,44 +48,42 @@ export async function persistQueryClient({
4848 hydrateOptions,
4949 dehydrateOptions,
5050} : PersistQueryClientOptions ) {
51- if ( typeof window !== 'undefined' ) {
52- // Subscribe to changes
53- const saveClient = ( ) => {
54- const persistClient : PersistedClient = {
55- buster,
56- timestamp : Date . now ( ) ,
57- clientState : dehydrate ( queryClient , dehydrateOptions ) ,
58- }
59-
60- persistor . persistClient ( persistClient )
51+ // Subscribe to changes
52+ const saveClient = ( ) => {
53+ const persistClient : PersistedClient = {
54+ buster,
55+ timestamp : Date . now ( ) ,
56+ clientState : dehydrate ( queryClient , dehydrateOptions ) ,
6157 }
6258
63- // Attempt restore
64- try {
65- const persistedClient = await persistor . restoreClient ( )
59+ persistor . persistClient ( persistClient )
60+ }
6661
67- if ( persistedClient ) {
68- if ( persistedClient . timestamp ) {
69- const expired = Date . now ( ) - persistedClient . timestamp > maxAge
70- const busted = persistedClient . buster !== buster
71- if ( expired || busted ) {
72- persistor . removeClient ( )
73- } else {
74- hydrate ( queryClient , persistedClient . clientState , hydrateOptions )
75- }
76- } else {
62+ // Attempt restore
63+ try {
64+ const persistedClient = await persistor . restoreClient ( )
65+
66+ if ( persistedClient ) {
67+ if ( persistedClient . timestamp ) {
68+ const expired = Date . now ( ) - persistedClient . timestamp > maxAge
69+ const busted = persistedClient . buster !== buster
70+ if ( expired || busted ) {
7771 persistor . removeClient ( )
72+ } else {
73+ hydrate ( queryClient , persistedClient . clientState , hydrateOptions )
7874 }
75+ } else {
76+ persistor . removeClient ( )
7977 }
80- } catch ( err ) {
81- getLogger ( ) . error ( err )
82- getLogger ( ) . warn (
83- 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.'
84- )
85- persistor . removeClient ( )
8678 }
87-
88- // Subscribe to changes in the query cache to trigger the save
89- queryClient . getQueryCache ( ) . subscribe ( saveClient )
79+ } catch ( err ) {
80+ getLogger ( ) . error ( err )
81+ getLogger ( ) . warn (
82+ 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.'
83+ )
84+ persistor . removeClient ( )
9085 }
86+
87+ // Subscribe to changes in the query cache to trigger the save
88+ queryClient . getQueryCache ( ) . subscribe ( saveClient )
9189}
0 commit comments