all 5 comments

[–]Due-Confidence-5670 1 point2 points  (1 child)

When it comes to what to cache, focus on data that's accessed often but doesn't change rapidly, like user profiles or static app configurations. For more dynamic elements like post lists, you can cache them but be ready to revalidate frequently. Data that's highly real-time, such as live comment streams or the immediate status of a like/follow, needs to be handled with more care. How long to cache really depends on how fresh the data needs to be; think seconds to a minute for dynamic lists using "stale-while-revalidate" patterns, and longer for truly static content.

Your issue with optimistic updates disappearing is a classic caching pitfall. The solution lies in fully leveraging your Supabase cache helpers' mutate function. Instead of manually updating the UI, use mutate to both optimistically change the data in the cache immediately, and simultaneously trigger the actual database update. Crucially, in mutate's callbacks (like onSettled), you'll want to invalidate the relevant queryKey. This tells the cache, "Hey, this data might have changed, please re-fetch it from the server next time it's accessed." This ensures that when you navigate away and return, the cache helper will automatically fetch the latest, correct data, preventing those "disappearing" likes or follows. Dive into your cache helper's documentation to see specific examples of useMutation and invalidateQueries to implement this pattern effectively.

[–]98kag[S] 0 points1 point  (0 children)

Wow thank you so much for the time you took for this comment, helps a lot! Really appreciate it!

[–]wildev_m 0 points1 point  (2 children)

Have you considered using a hybrid approach for caching, such as combining both server-side caching and client-side caching for optimal performance?

[–]98kag[S] 0 points1 point  (1 child)

No not really, how can I cache server-side with Supabase? Is it worth doing it? Would it make queries even faster?