Skip to content

Commit 8141a29

Browse files
committed
fix(persist): remove environment check
because we can now pass in undefined for persisters during SSR, and the persisters themselves are not window related anymore, as we don't use window.localStorage directly
1 parent 0c04e7d commit 8141a29

File tree

4 files changed

+41
-48
lines changed

4 files changed

+41
-48
lines changed

docs/src/pages/plugins/persistQueryClient.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ const queryClient = new QueryClient({
3131
})
3232
```
3333

34-
### Environment Checking
35-
A check for window `undefined` is performed prior to saving/restoring/removing your data (avoids build errors).
36-
3734
### Cache Busting
3835

3936
Sometimes you may make changes to your application or data that immediately invalidate any and all cached data. If and when this happens, you can pass a `buster` string option. If the cache that is found does not also have that buster string, it will be discarded. The following several functions accept this option:

src/createAsyncStoragePersister/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export type AsyncPersistRetryer = (props: {
1515
}) => Promisable<PersistedClient | undefined>
1616

1717
interface CreateAsyncStoragePersisterOptions {
18-
/** The storage client used for setting an retrieving items from cache */
18+
/** The storage client used for setting and retrieving items from cache.
19+
* For SSR pass in `undefined`.
20+
*/
1921
storage: AsyncStorage | undefined
2022
/** The key to use when storing the cache */
2123
key?: string

src/createWebStoragePersister/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
} from '../persistQueryClient'
77

88
interface CreateWebStoragePersisterOptions {
9-
/** The storage client used for setting and retrieving items from cache */
9+
/** The storage client used for setting and retrieving items from cache.
10+
* For SSR pass in `undefined`.
11+
*/
1012
storage: Storage | undefined
1113
/** The key to use when storing the cache */
1214
key?: string

src/persistQueryClient/persist.ts

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,32 @@ export async function persistQueryClientRestore({
6666
buster = '',
6767
hydrateOptions,
6868
}: PersistedQueryClientRestoreOptions) {
69-
if (typeof window !== 'undefined') {
70-
try {
71-
const persistedClient = await persister.restoreClient()
72-
73-
if (persistedClient) {
74-
if (persistedClient.timestamp) {
75-
const expired = Date.now() - persistedClient.timestamp > maxAge
76-
const busted = persistedClient.buster !== buster
77-
if (expired || busted) {
78-
persister.removeClient()
79-
} else {
80-
hydrate(queryClient, persistedClient.clientState, hydrateOptions)
81-
}
82-
} else {
69+
try {
70+
const persistedClient = await persister.restoreClient()
71+
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) {
8377
persister.removeClient()
78+
} else {
79+
hydrate(queryClient, persistedClient.clientState, hydrateOptions)
8480
}
81+
} else {
82+
persister.removeClient()
8583
}
86-
} catch (err) {
87-
if (process.env.NODE_ENV !== 'production') {
88-
queryClient.getLogger().error(err)
89-
queryClient
90-
.getLogger()
91-
.warn(
92-
'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.'
93-
)
94-
}
95-
persister.removeClient()
9684
}
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()
9795
}
9896
}
9997

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

122118
/**
@@ -157,17 +153,13 @@ export function persistQueryClient(
157153
persistQueryClientUnsubscribe?.()
158154
}
159155

160-
let restorePromise = Promise.resolve()
161-
162-
if (typeof window !== 'undefined') {
163-
// Attempt restore
164-
restorePromise = persistQueryClientRestore(props).then(() => {
165-
if (!hasUnsubscribed) {
166-
// Subscribe to changes in the query cache to trigger the save
167-
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)
168-
}
169-
})
170-
}
156+
// Attempt restore
157+
const restorePromise = persistQueryClientRestore(props).then(() => {
158+
if (!hasUnsubscribed) {
159+
// Subscribe to changes in the query cache to trigger the save
160+
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)
161+
}
162+
})
171163

172164
return [unsubscribe, restorePromise]
173165
}

0 commit comments

Comments
 (0)