Deduplication of requests using Realtime with Supabase
So I'm using Realtime with Supabase and I have a component that uses data from 2 separate tables.
I have an RPC at
/api/analysis which aggregates data from both of these tables.
I have created a subscription to each individual table which then invalidates the queryKey: ["analysis"] whenever rows in the respective tables change.
Whilst this does work, it is making duplicate requests.
Since one of the tables has a nested structure, whenever a parent row is deleted, all of it's children are also removed.
Supabase then fires a message for each removed row which then calls invalidateQueries x times.
Is deduplication supposed work in this case, or is there a better alternative approach?2 Replies
modern-teal•3y ago
Before invalidating, you could potentially use the
dataUpdatedAt field of the query (get the query using queryCache.find()) to somehow check whether the query data is "fresh" enough to indicate it's been invalidated recently.
Might be better to handle this outside of tanstack query though, like putting the invalidation logic in a function that you could throttle.foreign-sapphireOP•3y ago
Your suggestion led me to utilise the
queryCache().findAll() and discover the Query.state.isInvalidated field.
Then I call queryClient.invalidateQueries() only if it hasn't been invalidated yet.
This seems to work well, so hopefully I won't have to throttle it outside TQ just yet.
Thank you for your help!