-
I faced the issue with high memory consumption on nodejs app with SSR and [email protected]. Profiling the memory leads to the react-query cache internals. As I understand, a cache for the server works as explained in docs with isolation for every request. The problem is that cache for every request stays in server memory for the Explicit call to queryClient.clear() after sending the response fixes the problem, but it isn't mentioned in docs for ssr Questions: Is this intentional how the cache works on server? Is call to |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 15 replies
-
are you creating the queryClient inside of getServerSideProps / getStaticProps?
but yeah the running timers might actually stop it from being garbage collected by node. Have you tried setting a smaller cacheTime, like 10 seconds? 0 won't work because then it could be empty by the time you call
Can you share the profiling results maybe? |
Beta Was this translation helpful? Give feedback.
-
@TkDodo @meskill |
Beta Was this translation helpful? Give feedback.
-
Same question as @phaethon5882 ...
Thanks ! |
Beta Was this translation helpful? Give feedback.
-
I think this new v4 update for setting cacheTime to be Infinity or query.clear() only works if not passing in any custom cacheTime The memory leak issue will still happen if useQuery is used inside components with custom cacheTime while being rendered in ssr. I tried to have a root clean up method where we pass queryClient to QueryClientProvider useEffect(() => {
return () => {
queryClient.clear()
}
}, []) but it still didn't solve the problem. I then manually change where we customize cacheTime to be cacheTime: typeof window !== 'undefined' ? Infinity: YOUR_OWN_CUSTOM_TIME which seems working @TkDodo do you think it makes sense that react-query enforce cacheTime to be Infinity by default on server side regardless of what custom cache time user pass in? |
Beta Was this translation helpful? Give feedback.
are you creating the queryClient inside of getServerSideProps / getStaticProps?
but yeah the running timers might actually stop it from being garbage collected by node. Have you tried setting a smaller cacheTime, like 10 seconds? 0 won't work because then it could be empty by the time you call
dehydrate
...Can you share the profiling results maybe?