How can I use a localStorage persister?

I wanna add a localStorage persister, but I'm getting errors for hydration:
const persister = createSyncStoragePersister({
storage: window.localStorage,
throttleTime: 1000,
retry: removeOldestQuery,
serialize: data => JSON.stringify(data),
deserialize: data => JSON.parse(data) as PersistedClient
})

...
return (
<PersistQueryClientProvider
client={queryClient}
persistOptions={{ persister }}
>
<apiClient.Provider client={trpcClient} queryClient={queryClient}>
{props.children}
...
const persister = createSyncStoragePersister({
storage: window.localStorage,
throttleTime: 1000,
retry: removeOldestQuery,
serialize: data => JSON.stringify(data),
deserialize: data => JSON.parse(data) as PersistedClient
})

...
return (
<PersistQueryClientProvider
client={queryClient}
persistOptions={{ persister }}
>
<apiClient.Provider client={trpcClient} queryClient={queryClient}>
{props.children}
...
ERROR:
const persister = createSyncStoragePersister({
> 59 | storage: window.localStorage,
| ^
60 | throttleTime: 1000,
61 | retry: removeOldestQuery,
62 | serialize: data => JSON.stringify(data),
const persister = createSyncStoragePersister({
> 59 | storage: window.localStorage,
| ^
60 | throttleTime: 1000,
61 | retry: removeOldestQuery,
62 | serialize: data => JSON.stringify(data),
6 Replies
NickServ
NickServ13mo ago
You can't use that on a server, it requires a client
gwilliamnn
gwilliamnnOP13mo ago
I know, this is the client Provider...
NickServ
NickServ13mo ago
You're still rendering it on a server without checking if window exists
TheDarkKnight
TheDarkKnight10mo ago
@gwilliamnn Could you solve this?
lanc3
lanc310mo ago
You'll have to do something like this
const localStorage = typeof window !== "undefined" ? window.localStorage : null;
const localStorage = typeof window !== "undefined" ? window.localStorage : null;
really depends though on how your application works
Jingles
Jingles6mo ago
any progress?
Want results from more Discord servers?
Add your server