I'm rewriting a table component that supports column sorting (asc/desc). The original was fully server-side rendered and used a server action to fetch data. This worked okay with a mock API, but once hooked to the real one, the performance dropped, as calling the server action on every sort rerendered the page and became too slow. Caching isn’t viable due to data size and user volume.
I moved the table to a client component, which was easy, but now I’m stuck on data fetching. I created a wrapper around the table where tried passing a callback that calls a server action returning a promise, and then use-ing that promise in the component. But unfortunately I found out server actions can’t be called during render (why?).
I can’t move the fetch logic client-side because it needs access to secrets/env vars. I know I could use an API route instead, but I really dislike writing fetch logic, handling the errors, etc, and losing the simplicity of server action just being a simple function call, where it's all handled for you.
Is there any way to call a server action during render? Or a better pattern for client components getting data from the server-side data without constantly re-fetching?