Since these values are already available in scope since they're exports, we can avoid instantiating the object and passing to MDXLayout.
I would love this! The layoutProps logic seems to prevent .mdx pages in Next.js from exporting any of the data fetching functions because Next.js at some point removes those export statements, but MDX already included them in layoutProps, which causes a ReferenceError 馃槵
For people who stumble upon this and are looking for a quick fix, I wrote a Babel plugin which removes these three functions from layoutProps 馃榿 This is by no means an "official" workaround, I'm only speaking for myself.
// https://nextjs.org/docs/basic-features/data-fetching
const DATA_FETCH_FNS = ['getStaticPaths', 'getStaticProps', 'getServerProps']
module.exports = () => {
return {
visitor: {
ObjectProperty(path) {
if (
DATA_FETCH_FNS.includes(path.node.value.name) &&
path.findParent(
(path) =>
path.isVariableDeclarator() &&
path.node.id.name === 'layoutProps',
)
) {
path.remove()
}
},
},
}
}
Copy this into a file and add it to your Babel configuration:
// babel.config.js
module.exports = {
presets: [
'next/babel',
],
plugins: [
'./etc/babel-plugin-nextjs-mdx-patch',
],
}
I'm using mdx to define pages in Next.js, via its pageExtensions: ['mdx'] option. Currently, I'm setting the page title by including export const title = 'Foo' within a .mdx file, and then using a custom wrapper like this:
import Head from 'next/head';
const Wrapper = ({ children, title }) => (
<main>
<Head>
<title>{title}</title>
</Head>
{children}
</main>
);
This works because the layoutProps are passed to Wrapper as props. If they're removed, is there a different solution that I could use for the same effect?
Closed by #1199 (v2 branch)
Most helpful comment
I would love this! The
layoutPropslogic seems to prevent.mdxpages in Next.js from exporting any of the data fetching functions because Next.js at some point removes those export statements, but MDX already included them inlayoutProps, which causes aReferenceError馃槵