Created
March 24, 2020 15:39
-
-
Save benawad/20c6c8f21dced3a31401c8eb750f9f62 to your computer and use it in GitHub Desktop.
ssg + mdx next.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.