Components referenced after running compiled code #2307
-
What I'm trying to achieveI'm trying to run a MDX string which has a component in it. This component contains a reference to a next/link. I'm trying to follow using on-demand mdxjs instructions. Here is the component: import Link from 'next/link';
import React from 'react'
const Component1 = () => {
return (
<div>
<Link href={"/"}>
I'm a LINK
</Link>
</div>
)
}
export default Component1 Here is the Page that tries to compile and run: import { useState, useEffect, Fragment } from "react";
//import * as runtime from 'react/jsx-runtime' // Production.
import * as runtime from "react/jsx-dev-runtime"; // Development.
import { compile, run } from "@mdx-js/mdx";
import Component1 from "../components/SimpleComponent";
import type { MDXModule } from "mdx/types";
const stringMDX = `
<Component1 />
`;
export default function Page({ code }: any) {
const [mdxModule, setMdxModule] = useState<MDXModule>();
const Content = mdxModule ? mdxModule?.default : Fragment;
useEffect(() => {
(async () => {
setMdxModule(await run(code, runtime));
})();
}, [code]);
return (
<>
{code && <Content components={{ Component1 }} />}
</>
);
}
export async function getStaticProps() {
const code = String(
await compile(stringMDX, {
outputFormat: "function-body",
development: process.env.NODE_ENV === "production" ? false : true,
})
);
return { props: { code } };
} What happenedClient side error after build, however, while in dev, the error does not appear, according to the attached codesandbox. What I was expectingNavigation to work on production as it does in development mode. Am I missing something? (PROD) Deployment Link: https://nex-link-clientside.vercel.app/ |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I dunno who owns |
Beta Was this translation helpful? Give feedback.
-
You get an error due to a difference between development and production. |
Beta Was this translation helpful? Give feedback.
You get an error due to a difference between development and production.
This is explained in our guide: https://mdxjs.com/guides/mdx-on-demand/#nextjs-example.
To solve the error you see, follow that guide, and explicitly set the
development
option passed tocompile
.