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 ...@@ -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. 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 ```js
import dynamic from 'next/dynamic' import dynamic from 'next/dynamic'
// ... const SomeHeavyComponent = dynamic(() => import('SomeHeavyComponent'))
export async function getStaticProps() {
const { content, data } = matter(source)
// ...
export function SomePage({ mdxSource, componentNames }) {
const components = { const components = {
...defaultComponents, ...defaultComponents,
SomeHeavyComponent: /<SomeHeavyComponent/.test(content) SomeHeavyComponent: componentNames.includes('SomeHeavyComponent')
? dynamic(() => import('SomeHeavyComponent')) ? SomeHeavyComponent
: null, : 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 @@ ...@@ -10,10 +10,10 @@
"dependencies": { "dependencies": {
"gray-matter": "^4.0.2", "gray-matter": "^4.0.2",
"next": "latest", "next": "latest",
"next-mdx-remote": "^1.0.0", "next-mdx-remote": "^3.0.1",
"next-remote-watch": "0.2.0", "next-remote-watch": "1.0.0",
"react": "^16.13.1", "react": "^17.0.2",
"react-dom": "^16.13.1" "react-dom": "^17.0.2"
}, },
"license": "MIT" "license": "MIT"
} }
import fs from 'fs' import fs from 'fs'
import matter from 'gray-matter' import matter from 'gray-matter'
import hydrate from 'next-mdx-remote/hydrate' import { MDXRemote } from 'next-mdx-remote'
import renderToString from 'next-mdx-remote/render-to-string' import { serialize } from 'next-mdx-remote/serialize'
import dynamic from 'next/dynamic' import dynamic from 'next/dynamic'
import Head from 'next/head' import Head from 'next/head'
import Link from 'next/link' import Link from 'next/link'
...@@ -24,7 +24,6 @@ const components = { ...@@ -24,7 +24,6 @@ const components = {
} }
export default function PostPage({ source, frontMatter }) { export default function PostPage({ source, frontMatter }) {
const content = hydrate(source, { components })
return ( return (
<Layout> <Layout>
<header> <header>
...@@ -40,7 +39,9 @@ export default function PostPage({ source, frontMatter }) { ...@@ -40,7 +39,9 @@ export default function PostPage({ source, frontMatter }) {
<p className="description">{frontMatter.description}</p> <p className="description">{frontMatter.description}</p>
)} )}
</div> </div>
<main>{content}</main> <main>
<MDXRemote {...source} components={components} />
</main>
<style jsx>{` <style jsx>{`
.post-header h1 { .post-header h1 {
...@@ -64,8 +65,7 @@ export const getStaticProps = async ({ params }) => { ...@@ -64,8 +65,7 @@ export const getStaticProps = async ({ params }) => {
const { content, data } = matter(source) const { content, data } = matter(source)
const mdxSource = await renderToString(content, { const mdxSource = await serialize(content, {
components,
// Optionally pass remark/rehype plugins // Optionally pass remark/rehype plugins
mdxOptions: { mdxOptions: {
remarkPlugins: [], 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