Which Data fetching approach is better?
Unanswered
Chilean jack mackerel posted this in #help-forum
Chilean jack mackerelOP
I'm trying to design my project structure and I'm thinking about how I need to handle database requests for my project. I have a supabase database so it comes with good ORM. Now the case is that I don't know which approach is better:
* 1: Fetch the data with directly from supabase-js client on the server side, so the initial page load will be longer.
* 2: Create an API route that fetches data from supabase and returns result, make it client-sided and use something like react-query to cache the results. (It'll block the main thread for a while I guess)
* 1: Fetch the data with directly from supabase-js client on the server side, so the initial page load will be longer.
* 2: Create an API route that fetches data from supabase and returns result, make it client-sided and use something like react-query to cache the results. (It'll block the main thread for a while I guess)
3 Replies
Lionhead
I am using the same tech stack in a personal project, and have been researching the same question for a couple weeks now. My personal opinion is that Server Actions are the best approach for server side data fetching, because they allow to achieve all of Next JS recommendations (fetching data on the server, AND pulling / displaying it in server components, while allowing access in client components as well with AWESOME on demand invalidation techniques and auto reload). However, as I'm sure you are aware, Server Actions are currently in Alpha and considered experimental / not production ready. I have found this EXTREMELY frustrating, because it seems as if they released 13.4 as 'production ready', but they did so missing very important features that only Server Actions thoroughly provide (fetching data directly in server components can be inconvenient to pass data through prop drilling to client components if needed, and on-demand invalidation is not available - also, route handlers are intended to only be called from client components, so not ideal if you want to display the fetched data in a server component). My solution to the problem is to, for now, just continue to use React Query and fetch data on the client. Once Server Actions become production ready, they can be incorporated into the query action function, or replace react query altogether. I would be curious to hear your thoughts on all this, as it definitely is hard to wrap my head around all of it 😂
As another note, supabase has released some helpers to allow for server side auth that seems pretty nice. Also, react query released a Hydrate component / feature that allows initial server-side data fetching, with all additional requests on the client (so for mutations and invalidation). While I can't say for certain, I am personally leaning in the direction of continuing with client-side data fetching with react query until the server actions reach production.