Skip to content

Commit 35b08bc

Browse files
Remove checks for window in persistQueryClient
1 parent 128c5e2 commit 35b08bc

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

src/persistQueryClient/index.ts

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,32 @@ export async function persistQueryClientRestore({
6565
buster = '',
6666
hydrateOptions,
6767
}: PersistedQueryClientRestoreOptions) {
68-
if (typeof window !== 'undefined') {
69-
try {
70-
const persistedClient = await persister.restoreClient()
68+
try {
69+
const persistedClient = await persister.restoreClient()
7170

72-
if (persistedClient) {
73-
if (persistedClient.timestamp) {
74-
const expired = Date.now() - persistedClient.timestamp > maxAge
75-
const busted = persistedClient.buster !== buster
76-
if (expired || busted) {
77-
persister.removeClient()
78-
} else {
79-
hydrate(queryClient, persistedClient.clientState, hydrateOptions)
80-
}
81-
} else {
71+
if (persistedClient) {
72+
if (persistedClient.timestamp) {
73+
const expired = Date.now() - persistedClient.timestamp > maxAge
74+
const busted = persistedClient.buster !== buster
75+
if (expired || busted) {
8276
persister.removeClient()
77+
} else {
78+
hydrate(queryClient, persistedClient.clientState, hydrateOptions)
8379
}
80+
} else {
81+
persister.removeClient()
8482
}
85-
} catch (err) {
86-
if (process.env.NODE_ENV !== 'production') {
87-
queryClient.getLogger().error(err)
88-
queryClient
89-
.getLogger()
90-
.warn(
91-
'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.'
92-
)
93-
}
94-
persister.removeClient()
9583
}
84+
} catch (err) {
85+
if (process.env.NODE_ENV !== 'production') {
86+
queryClient.getLogger().error(err)
87+
queryClient
88+
.getLogger()
89+
.warn(
90+
'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.'
91+
)
92+
}
93+
persister.removeClient()
9694
}
9795
}
9896

@@ -107,15 +105,13 @@ export async function persistQueryClientSave({
107105
buster = '',
108106
dehydrateOptions,
109107
}: PersistedQueryClientSaveOptions) {
110-
if (typeof window !== 'undefined') {
111-
const persistClient: PersistedClient = {
112-
buster,
113-
timestamp: Date.now(),
114-
clientState: dehydrate(queryClient, dehydrateOptions),
115-
}
116-
117-
await persister.persistClient(persistClient)
108+
const persistClient: PersistedClient = {
109+
buster,
110+
timestamp: Date.now(),
111+
clientState: dehydrate(queryClient, dehydrateOptions),
118112
}
113+
114+
await persister.persistClient(persistClient)
119115
}
120116

121117
/**
@@ -135,11 +131,9 @@ export function persistQueryClientSubscribe(
135131
* (Retained for backwards compatibility)
136132
*/
137133
export async function persistQueryClient(props: PersistQueryClientOptions) {
138-
if (typeof window !== 'undefined') {
139-
// Attempt restore
140-
await persistQueryClientRestore(props)
134+
// Attempt restore
135+
await persistQueryClientRestore(props)
141136

142-
// Subscribe to changes in the query cache to trigger the save
143-
return persistQueryClientSubscribe(props)
144-
}
137+
// Subscribe to changes in the query cache to trigger the save
138+
return persistQueryClientSubscribe(props)
145139
}

0 commit comments

Comments
 (0)