Add support for React Server Components #331
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A small note before the explaining the changes:
This PR is not an endorsement from me for using remote MDX.
Before using MDX as a remote content source it's important to know that the way MDX works is by first compiling the MDX to JavaScript and then executing it. What this means in practice is that MDX is similar to fetching a JavaScript file and then executing it on your server which can lead to remote code execution (RCE) as the MDX file can run any JavaScript.
This is especially important to keep in mind when the source of the MDX content is an external party.
With that explained this is still a commonly used package in the ecosystem which is why I'm opening this PR.
This PR introduces a backwards compatible change that allows the following usage:
It introduces a separate export under
next-mdx-remote/rsc
because the exported component is slightly different:async function
React Server Componentlazy
as that relied on it being a client component (useState, useEffect)async function
React Component, if there's a suspense boundary above or around the MDX content you can show a loading stateExamples
Default usage
Showing a loading state while the source is processed
Providing a list of custom components
Frontmatter
The current setup has a two-step process where you first
serialize
and then pass those props to the React component. With Server Components that is no longer needed and you can use<MDXRemote>
with asource
property directly. However that means the frontmatter can only be used as part of the MDX source and not to render separate elements.In order to solve that case there is a separate
compileMDX
function exported that can be executed in a server component to get both the content and the frontmatter.compileMDX
takes exactly the same props as<MDXRemote>
.