Add support for React Server Components#331
Conversation
|
Added the fixtures needed for the tests but because of the way the package itself is not isolated running it seems to break the app. Seems the test suite should be updated to use a standalone version of the package, you can have a look at the Next.js test suite / copy that behavior, unfortunately I don't have time right now to port all that over. |
|
Plan for tests so I don't forget. This gets around the error when importing from
I'll work on this tomorrow so we can get a release out with your change here. Thanks! |
|
@brkalow potentially helpful this is the util we use for Next.js to create standalone directories for tests: https://github.com/vercel/next.js/blob/canary/test/lib/e2e-utils.ts#L124 |
|
Going to merge this as-is and follow-up with docs and test updates, thanks again! 🙏 |
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/rscbecause the exported component is slightly different:async functionReact Server Componentlazyas that relied on it being a client component (useState, useEffect)async functionReact 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
serializeand then pass those props to the React component. With Server Components that is no longer needed and you can use<MDXRemote>with asourceproperty 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
compileMDXfunction exported that can be executed in a server component to get both the content and the frontmatter.compileMDXtakes exactly the same props as<MDXRemote>.