@@ -48,44 +48,42 @@ export async function persistQueryClient({
48
48
hydrateOptions,
49
49
dehydrateOptions,
50
50
} : 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 ) ,
61
57
}
62
58
63
- // Attempt restore
64
- try {
65
- const persistedClient = await persistor . restoreClient ( )
59
+ persistor . persistClient ( persistClient )
60
+ }
66
61
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 ) {
77
71
persistor . removeClient ( )
72
+ } else {
73
+ hydrate ( queryClient , persistedClient . clientState , hydrateOptions )
78
74
}
75
+ } else {
76
+ persistor . removeClient ( )
79
77
}
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 ( )
86
78
}
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 ( )
90
85
}
86
+
87
+ // Subscribe to changes in the query cache to trigger the save
88
+ queryClient . getQueryCache ( ) . subscribe ( saveClient )
91
89
}
0 commit comments