hashicorp / next-mdx-remote

Load MDX content from anywhere
Mozilla Public License 2.0
2.67k stars 140 forks source link

The parseFrontmatter option doesn't work with next-mdx-remote/rsc #345

Closed jasongerbes closed 1 year ago

jasongerbes commented 1 year ago

Expected behaviour

Using the compileMDX method of next-mdx-remote/rsc with parseFrontmatter: true, the expected behaviour is that any frontmatter should be parsed from the MDX source.

image

Actual behaviour

The parseFrontmatter option has no affect on the content returned by compileMDX, and the frontmatter value is always {}.

image

Reproduction steps

Minimal reproduction here: https://github.com/jasongerbes/next-mdx-remote-rsc

This reproduction uses the "Access Frontmatter outside of MDX" example from the README with a couple of fixes:

  1. Added missing await
  2. Removed extra "Hello World!" heading
// app/page.tsx
import { compileMDX } from "next-mdx-remote/rsc";

export default async function Home() {
  const { content, frontmatter } = await compileMDX({
    source: `
      ---
      title: RSC Frontmatter Example
      ---
      This is from Server Components!
    `,
    options: { parseFrontmatter: true },
  });
  return (
    <>
      <h1>{frontmatter.title}</h1>
      {content}
    </>
  );
}
chrisweb commented 1 year ago

I can confirm your findings and I think I found the problem, there is space between the first line of frontmatter "---" and the opening of the template literal, if you change the example to this:

  const { content, frontmatter } = await compileMDX({
    source: `---
      title: RSC Frontmatter Example
      ---
      This is from Server Components!`,
    options: { parseFrontmatter: true },
  });

then it should work

on top of the readme the example for the pages directory getStaticProps is correct the new ones are not

I got suspicious when I tried to load the content from a file and it actually worked with the same MDX file but when using the template literal it didn't anymore, so I knew it had something to do with its formatting

jasongerbes commented 1 year ago

@chrisweb you're right that it was an issue with whitespace. However, you also need to remove the whitespace before the other lines of the source:

  const { content, frontmatter } = await compileMDX({
    source: `---
title: RSC Frontmatter Example
---
This is from Server Components!`,
    options: { parseFrontmatter: true },
  });