rsnext/examples/with-mdx-remote
Evan Ye 030eae17bd
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).
2021-05-11 03:40:48 +00:00
..
components Add with-mdx-remote example (#16613) 2020-08-29 01:22:35 +00:00
pages Update with-mdx-remote example (#24973) 2021-05-11 03:40:48 +00:00
posts Add with-mdx-remote example (#16613) 2020-08-29 01:22:35 +00:00
utils Add with-mdx-remote example (#16613) 2020-08-29 01:22:35 +00:00
.gitignore Add with-mdx-remote example (#16613) 2020-08-29 01:22:35 +00:00
package.json Update with-mdx-remote example (#24973) 2021-05-11 03:40:48 +00:00
README.md Update with-mdx-remote example (#24973) 2021-05-11 03:40:48 +00:00

MDX Remote Example

This example shows how a simple blog might be built using the next-mdx-remote library, which allows mdx content to be loaded via getStaticProps or getServerSideProps. The mdx content is loaded from a local folder, but it could be loaded from a database or anywhere else.

The example also showcases next-remote-watch, a library that allows next.js to watch files outside the pages folder that are not explicitly imported, which enables the mdx content here to trigger a live reload on change.

Since next-remote-watch uses undocumented Next.js APIs, it doesn't replace the default dev script for this example. To use it, run npm run dev:watch or yarn dev:watch.

Deploy your own

Deploy the example using Vercel:

Deploy with Vercel

How to use

Execute create-next-app with npm or Yarn to bootstrap the example:

npx create-next-app --example with-mdx-remote with-mdx-remote-app
# or
yarn create next-app --example with-mdx-remote with-mdx-remote-app

Deploy it to the cloud with Vercel (Documentation).

Notes

Conditional custom components

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 pass a list of component names, checking the names in the page render function to see which components need to be dynamically loaded.

import dynamic from 'next/dynamic'

const SomeHeavyComponent = dynamic(() => import('SomeHeavyComponent'))

// ...
export function SomePage({ mdxSource, componentNames }) {
  const components = {
    ...defaultComponents,
    SomeHeavyComponent: componentNames.includes('SomeHeavyComponent')
      ? SomeHeavyComponent
      : null,
  }

  return <MDXRemote {...mdxSource} />
}

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,
    },
  }
}