Replies: 4 comments 1 reply
-
Update - we've found that we can return RSCs on-demand from server actions, which might solve for this whole thing! |
Beta Was this translation helpful? Give feedback.
-
I agree this would be an amazing feature to have! The way I think about it is that server components are currently excellent for fetching data that corresponds to a page URL, but there are many times when client-side interactivity requires fetching data independent of the page URL. Some examples:
Being able to import a server component into a child component (not just pass a server component to a child component) would enable these uses cases without have to do a cumbersome client-side fetch + API dance.
Unfortunately, server actions "are not recommended for data fetching". They use In order to support importing a server component into a child component, there are some open questions that I think need to be answered. For example:
I would love to see more discussion about this. Like I said, I think it would be an incredible feature and would significantly boost the usage of server components. |
Beta Was this translation helpful? Give feedback.
-
I took a shot at a userland implementation of this functionality, which I termed "remote components". You can see it in action at https://next-remote-components-v1.vercel.app/. It supports static typing, client components as children, suspense boundaries, and refs to elements. To use remote components in your own project, copy
The current implementation has some caveats, but they are addressable. The most noticeable shortcoming is the lack of prerendering. If the implementation were in React itself (and/or Next.js) instead of userland, it would be straightforward to address these issues and further improve DX. For that reason, I am strongly in favor of implementing remote components in React itself. I think it would be a very compelling feature. |
Beta Was this translation helpful? Give feedback.
-
This is easy if we have API to render a RSC from API route. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Non-Goals
Background
Lots of highly dynamic admin UIs feature drawers / modals that can be opened in a "nested" or recursive fashion up to an unknown level, based on runtime data which is not statically predictable.
Here is a simple example of Payload showing a recursive relationship field. This is a VERY simple / unlikely example, but will help illustrate the issue. Payload has lots of drawers / modals that need to render on-demand, based on client interactions. These modals can be recursive in nature. They contain server components that need to be rendered on the server (fields, views, etc).
recursive-modals.mp4
The Contentful admin panel does something similar, where you can "drill in" to relationship fields up to any level deep.
Right now, Payload renders all custom server components at the top level, so that they are ready to be used in client-side drawer interactions, but this is not ideal, because this means that RSCs cannot receive context-specific props. Consider a custom field component that is an RSC. We want to pass its
value
into the component as a prop, so the developer can use that value in their component. But, we can't pass the value, given that the RSC could be used in any number of drawers, each corresponding to a different document (thus a different value).Parallel Routes may seem like a potential solution to this, but due to their file-based routing, recursion to an unknown level is impossible.
If we could have some type of mechanism where we could dynamically request specific RSCs from the frontend, and then render them, this would solve for our use case.
Proposal
Not sure how this could be implemented. We would love to brainstorm this idea with the Next.js team.
One idea that comes to mind is some way to be able to import server components within client components, in a safe manner, such that they are available to render on-demand, but are not added to the client-side bundle. Maybe some directive could be created similar to
use client
, which would allow the import of server components in client components.Alternatively, if we could pass un-rendered server components as props to client components, that would also solve the issue.
Really though, we're open to any implementation mechanism!
Thank you for reading!
Beta Was this translation helpful? Give feedback.
All reactions