Trigger Server Component on Button Click (Fetch + Render Server-Side)
Unanswered
Bombay posted this in #help-forum
BombayOP
I’m working with Next.js App Router and trying to figure out a pattern for this workflow:
When a user clicks a button, I want to:
Fetch some data from the database (server-side)
Render a Server Component using that data
Send the rendered result back to the client and display it — without doing a full page reload
Basically:
I know Server Components can’t directly be called from the client, so I’m wondering what the right approach is here. Should I:
Use a Server Action that returns JSX (if that’s even possible)?
Or maybe call an API route / server action that returns the data and then hydrate a Client Component?
Or is there some hybrid pattern for this scenario?
Would love to hear how others are handling this kind of “on-demand server render” pattern 🙏
When a user clicks a button, I want to:
Fetch some data from the database (server-side)
Render a Server Component using that data
Send the rendered result back to the client and display it — without doing a full page reload
Basically:
Client triggers → Server fetch + render → Client shows rendered result
I know Server Components can’t directly be called from the client, so I’m wondering what the right approach is here. Should I:
Use a Server Action that returns JSX (if that’s even possible)?
Or maybe call an API route / server action that returns the data and then hydrate a Client Component?
Or is there some hybrid pattern for this scenario?
Would love to hear how others are handling this kind of “on-demand server render” pattern 🙏
4 Replies
@Bombay I’m working with Next.js App Router and trying to figure out a pattern for this workflow:
When a user clicks a button, I want to:
Fetch some data from the database (server-side)
Render a Server Component using that data
Send the rendered result back to the client and display it — without doing a full page reload
Basically:
> Client triggers → Server fetch + render → Client shows rendered result
I know Server Components can’t directly be called from the client, so I’m wondering what the right approach is here. Should I:
Use a Server Action that returns JSX (if that’s even possible)?
Or maybe call an API route / server action that returns the data and then hydrate a Client Component?
Or is there some hybrid pattern for this scenario?
Would love to hear how others are handling this kind of “on-demand server render” pattern 🙏
on click, update search params, which will hit the same endpoint but browser will "merge" changes in the dom so that it updates content without refreshing the whole page
BombayOP
thanks bro, but i like to show server-rendered component inside a dialog or sheet
@Bombay thanks bro, but i like to show server-rendered component inside a dialog or sheet
yes yes you can show server-rendered component inside a dialog or sheet with the method i described
"dialog" or "sheet" is just "content"