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
NickServ11mo ago
You can't use that on a server, it requires a client
gwilliamnn
gwilliamnn11mo ago
I know, this is the client Provider...
NickServ
NickServ11mo ago
You're still rendering it on a server without checking if window exists
TheDarkKnight
TheDarkKnight8mo ago
@gwilliamnn Could you solve this?
lanc3
lanc38mo 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
Jingles3mo ago
any progress?
Want results from more Discord servers?
Add your server