Go to file
Kasper Juul Hermansen 59607f81f6
Some checks reported errors
continuous-integration/drone/push Build encountered an error
continuous-integration/drone/pr Build encountered an error
Update all dependencies
2024-11-23 05:49:56 +00:00
posts/golang with blog 2022-10-31 22:44:13 +01:00
public Add docker 2022-10-12 22:31:15 +02:00
scripts Add cuddle 2022-10-12 22:39:45 +02:00
src add cuddle 2022-10-12 22:33:53 +02:00
templates Update all dependencies 2024-11-23 05:49:56 +00:00
.dockerignore with blog 2022-10-31 22:44:13 +01:00
.drone.yml with blog 2022-10-31 22:44:13 +01:00
.gitignore Add cuddle 2022-10-12 22:39:45 +02:00
.releaserc.yml Add cuddle 2022-10-12 22:39:45 +02:00
cuddle.yaml add scripts 2022-10-12 22:42:02 +02:00
Dockerfile Update all dependencies 2024-11-23 05:49:56 +00:00
next.config.js Add docker 2022-10-12 22:31:15 +02:00
package.json Update all dependencies 2024-11-23 05:49:56 +00:00
README.md Initial commit from Create Next App 2022-10-12 22:06:54 +02:00
renovate.json Add renovate.json 2022-10-25 20:54:10 +00:00
tsconfig.json Add base setup 2022-10-12 22:16:59 +02:00
yarn.lock Update all dependencies 2024-11-23 05:49:56 +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, Yarn, or pnpm to bootstrap the example:

npx create-next-app --example with-mdx-remote with-mdx-remote-app
yarn create next-app --example with-mdx-remote with-mdx-remote-app
pnpm 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'
import Test from '../components/test'

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

const defaultComponents = { Test }

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

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

export async function getStaticProps() {
  const source = `---
  title: Conditional custom components
  ---

  Some **mdx** text, with a default component <Test name={title}/> and a Heavy component <SomeHeavyComponent />
  `

  const { content, data } = matter(source)

  const componentNames = [
    /<SomeHeavyComponent/.test(content) ? 'SomeHeavyComponent' : null,
  ].filter(Boolean)

  const mdxSource = await serialize(content)

  return {
    props: {
      mdxSource,
      componentNames,
    },
  }
}