"getStaticProps is not defined" with mdx files
See original GitHub issueBug report
I’m trying to use nextjs and mdx to build a simple site. When I export getStaticProps, I get an “undefined” error. It looks like an issue that happens client side.
To Reproduce
I followed the “with-mdx” example to add mdx pages to my application. https://github.com/zeit/next.js/tree/canary/examples/with-mdx
I try to generate static props from the mdx using exports (https://mdxjs.com/getting-started#exports)
// src/pages/index.mdx
# Helloworld
Content is here!
export function getStaticProps() {
return {
props: {hello: 'world'}
}
}
// src/_app.tsx
...
export default function App(app: AppProps) {
const { Component, pageProps } = app;
return (<MDXProvider components={components}>
<pre>{JSON.stringify(pageProps)}</pre>
<Component {...pageProps} />
</MDXProvider>
);
}
I get an undefined error:
ReferenceError: getStaticProps is not defined
Module../src/pages/index.mdx
./src/pages/index.mdx:25
22 | const layoutProps = {
23 | layout,
24 | hello,
> 25 | getStaticProps
26 | };
27 | const MDXLayout = "wrapper"
28 | export default function MDXContent({
The <pre>{hello: "world"}</pre> appears on my webpage. It looks like this error is client side only, and the code behave as expect on the server.
Screenshot of the full error below.
Expected behavior
I expect to see the props and the content.
Screenshots
System information
- OS: macOS
- Version of Next.js: 9.3.4
- Version of Node.js: 12.14.0
"@mdx-js/loader": "^1.5.8",
"@next/mdx": "^9.3.5",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"isomorphic-unfetch": "^3.0.0",
"next": "^9.3.4",
"pg": "^8.0.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"swr": "^0.2.0",
"unfetch": "^4.1.0"
Thanks for supporting this project!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:12
- Comments:21 (9 by maintainers)
Top Results From Across the Web
Cannot import meta data from mdx file in getStaticProps nextjs
You can import metadata like follows. First, we export the metadata from within the .mdx file // in /pages/posts/example.mdx import Layout ...
Read more >Data Fetching: getStaticProps - Next.js
Learn how to use `getStaticProps` to generate static pages with Next.js. ... preview is true if the page is in the Preview Mode...
Read more >How to Build Your Own Blog with Next.js and MDX
MDX is more or less like the markdown files we always see in GitHub ... and upgrading of Next.js to fix this problem...
Read more >Building a Blog With Next.js and MDX | The WebStorm Blog
js app, let's set up Bootstrap in our app for styling. We'll use the Bootstrap CDN for this. Inside the ./pages/_app.js file, we'll...
Read more >MDX with Next.js: The Easiest Way to Build a Static Content Site
Getting MDX up and running with Next.js has never been easier thanks to Contentlayer: a content toolkit that makes working with content in ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I get a similar issue, but with
async, after writing:Rewriting to:
Just the same as you:
You have to export
getStaticPropsfrom the MDX page itself, not layout.There is one syntax that seems to circumvent this incompatibility between MDX and Next.js:
But I’d say that it’s somewhat of a bug in MDX that this works because all exports are meant to be passed as props to layout, e.g.
export const meta = { ... }is a common pattern, and I’d expectexport { meta } from './other-datato behave the same, but it doesn’t.