Unverified Commit 030eae17 authored by Evan Ye's avatar Evan Ye Committed by GitHub

Update with-mdx-remote example (#24973)


Update the with-mdx-remote example based on [next-mdx-remote](https://github.com/hashicorp/next-mdx-remote) v3.0, to make it faster and lighter-weight.

For more detail of API changes and migration guild, please refer to [v3.0.0 release note](https://github.com/hashicorp/next-mdx-remote/releases/tag/3.0.0).
parent e00e3c97
......@@ -30,25 +30,39 @@ Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&ut
When using `next-mdx-remote`, you can pass custom components to the MDX renderer. However, some pages/MDX files might use components that are used infrequently, or only on a single page. To avoid loading those components on every MDX page, you can use `next/dynamic` to conditionally load them.
For example, here's how you can change `getStaticProps` to conditionally add certain components:
For example, here's how you can change `getStaticProps` to pass a list of component names, checking the names in the page render function to see which components need to be dynamically loaded.
```js
import dynamic from 'next/dynamic'
// ...
export async function getStaticProps() {
const { content, data } = matter(source)
const SomeHeavyComponent = dynamic(() => import('SomeHeavyComponent'))
// ...
export function SomePage({ mdxSource, componentNames }) {
const components = {
...defaultComponents,
SomeHeavyComponent: /<SomeHeavyComponent/.test(content)
? dynamic(() => import('SomeHeavyComponent'))
SomeHeavyComponent: componentNames.includes('SomeHeavyComponent')
? SomeHeavyComponent
: null,
}
const mdxSource = await renderToString(content, { components })
return <MDXRemote {...mdxSource} />
}
```
If you do this, you'll also need to check in the page render function which components need to be dynamically loaded. You can pass a list of component names via `getStaticProps` to accomplish this.
export async function getStaticProps() {
const { content, data } = matter(source)
const componentNames = [
/<SomeHeavyComponent/.test(content) ? 'SomeHeavyComponent' : null,
].filter(Boolean)
const mdxSource = await serialize(content)
return {
props: {
mdxSource,
componentNames,
},
}
}
```
......@@ -10,10 +10,10 @@
"dependencies": {
"gray-matter": "^4.0.2",
"next": "latest",
"next-mdx-remote": "^1.0.0",
"next-remote-watch": "0.2.0",
"react": "^16.13.1",
"react-dom": "^16.13.1"
"next-mdx-remote": "^3.0.1",
"next-remote-watch": "1.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"license": "MIT"
}
import fs from 'fs'
import matter from 'gray-matter'
import hydrate from 'next-mdx-remote/hydrate'
import renderToString from 'next-mdx-remote/render-to-string'
import { MDXRemote } from 'next-mdx-remote'
import { serialize } from 'next-mdx-remote/serialize'
import dynamic from 'next/dynamic'
import Head from 'next/head'
import Link from 'next/link'
......@@ -24,7 +24,6 @@ const components = {
}
export default function PostPage({ source, frontMatter }) {
const content = hydrate(source, { components })
return (
<Layout>
<header>
......@@ -40,7 +39,9 @@ export default function PostPage({ source, frontMatter }) {
<p className="description">{frontMatter.description}</p>
)}
</div>
<main>{content}</main>
<main>
<MDXRemote {...source} components={components} />
</main>
<style jsx>{`
.post-header h1 {
......@@ -64,8 +65,7 @@ export const getStaticProps = async ({ params }) => {
const { content, data } = matter(source)
const mdxSource = await renderToString(content, {
components,
const mdxSource = await serialize(content, {
// Optionally pass remark/rehype plugins
mdxOptions: {
remarkPlugins: [],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment