Skip to content

Instantly share code, notes, and snippets.

@benawad
Created March 24, 2020 15:39
Show Gist options
  • Save benawad/20c6c8f21dced3a31401c8eb750f9f62 to your computer and use it in GitHub Desktop.
Save benawad/20c6c8f21dced3a31401c8eb750f9f62 to your computer and use it in GitHub Desktop.
ssg + mdx next.js
import fs from "fs";
import MDX from "@mdx-js/runtime";
import ReactDOM from "react-dom/server";
import path from "path";
const Post = ({ post }) => {
return <div dangerouslySetInnerHTML={{ __html: post }} />;
};
export async function getStaticPaths() {
return {
paths: fs
.readdirSync("posts")
.map(slug => ({ params: { slug: slug.replace(".mdx", "") } })),
fallback: false
};
}
export async function getStaticProps({ params: { slug } }) {
return {
props: {
post: ReactDOM.renderToStaticMarkup(
<MDX>{fs.readFileSync(path.join("posts", slug + ".mdx"))}</MDX>
)
}
};
}
@benawad
Copy link
Author

benawad commented Apr 5, 2020

@mdx-js/runtime won't run in the browser, so should be ok. I haven't looked much into MDX, so there might be a better way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment