React Query - Slow performance with many small cached requests

I have a list of accordions. Every accordion have items that contain image. On accordion click, I fetch items text data and image id to speed up list loading and load images later. Inside an item, I use image id to fetch an image per item. Image is in base64 format. In every accordion, I can have 10 or more items, so in total there can be few hundreds. When I open just one or two accordions and cache them, the app is working fine. CPU jumps to 20-30%. But if I open a lot of accordions and cache them (I can close them), next time I open accordion CPU usage jumps to 100%. List still works fluent - it displays data instantly, however if at this moment go to diffrent view in my app, request from this view will not be fetched for like few seconds. My guess is that react is rendering fine but when there are a lot of requested cached in react query than it checks them all to see if they need update or not. While cached is checked, new request (not cached ones) are blocked. This happens even if staleTime is infinite. If there is a way to optimize react query, that would be great. If not, I will probably try to counter this with by fetching images per accordion to make number of react query entries in cache smaller.
7 Replies
Christoph
Christoph16mo ago
I recommend using React Query Devtools, React Devtools & your browser's profiler to see what's actually going on. It's hard to say anything concrete on this basis.
Basilisk
Basilisk16mo ago
There are 3s of 15ms long checkIfSnapshotChanged functions calls in performance tab. In React profiler, there are plenty of: QueryRow key =[] which correspond to react query keys.
Basilisk
Basilisk16mo ago
I just noticed that those QueryRow key =[] are under ReactQueryDevtools. So I disabled devtools and now I hit max 20% on CPU. Seems that my CPU was spending all power on updating hidden devtools view. You can see on the screenshot that devtools takes 80% of time to render.
Develliot
Develliot16mo ago
If you used URLs to images and the images live on a CDN so the browser can load and cache them separately. I wouldn't encode images into base64 strings in an API call for react query to cache.
Basilisk
Basilisk16mo ago
I know that having just URLs for images instead of base64 would be much better and easier, but unfortunately, frontend doesn't have permissions to image's storage in my project right now. This is not a public app, so maybe this is the correct security approach, but if not let me know.
Develliot
Develliot16mo ago
I would have 2 image services a prod one that you care about and a dev/Integration one that you don't lax permissions etc and develop with the non prod one so just use env vars to know which one is which all you need is a place to dump the file and an returned URL S3 storage is cheap you could leave stuff there forever. Then you can keep your API res data trim and slim. You can use next config for a permanent redirect for anything /images/....blah.png to point at the cloudfront for the S3 bucket
Basilisk
Basilisk16mo ago
Just so you know I was using "@tanstack/react-query-devtools": "^4.0.10", after I updated to current newest "@tanstack/react-query-devtools": "^4.24.10" it is working much better with devtools enabled. However, it still suffers the same performance issues when opened. So it seems there was a bug that when devtools were hidden it was still rendered as if there were opened.
Want results from more Discord servers?
Add your server
More Posts
Can you run a NextJS project on a closed network?I would like to run a NextJS project in a closed network that doesn't have access to the internet. ASpreading Rest ArgumentsI'm attempting to write a function which eventually calls another function with the initially providWeird min-width: max-content behavior (jsfiddle)Can someone explain to me what's happening in this fiddle? https://jsfiddle.net/th147dmf/53/ This iDiscord/Slack page embeds not workingHello - I am trying to add some meta tags to my Next page so that the metadata is collected by DiscoPromise-returning function provided to attribute where a void return was expected.Hello, doing app with t3. I am using react-hook-form and I am getting error from handleSubmit: PromPrefetching more items than requested on client sideHey guys, I was wondering about prefetching a long list of items (100) with getServerSideProps, buHow can I cache blob image provided by backend (React)?I have a list of accordions. Every accordion have items that have images. On accordion click, I fetWhere to check if user is ownerHello, I have and app with three entities User, Deck and Card, and now for any operation on card/dDoes anyone know of a way to programmatically lift classNames into typescript intellisense?For example, if I import this component somewhere else, I'd like to be able to see the classNames thHow to make emit when some variable changes?I have a websockets server, in which i want to fire emit when there are n amount of users only ONCE.