-
I have been using How do I pass additional variables into the MDX scope in v2 when using Note: when rendering the MDX as part of an integration, I can work around the issue by directly importing it, e.g. import config from '/config/config'
.... However, when I render the same MDX using Let me describe my use case (to make sure what I'm trying to achieve is actually possible). Now comes the dynamic part: when the client loads the site, they make an API request to find out if any of the The rationale behind this is the following: rebuilding the website takes several minutes due running Is my use case too much outside of what MDXv2 was designed for? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Hey! You can pass It sounds like you’re regexing For your use case, you should likely do something along the lines of what’s described in the MDX on demand guide. You should be able to change an MDX file locally, push changes, and Next takes care of deploying your site? |
Beta Was this translation helpful? Give feedback.
-
Thank you for your reply! First, regarding the use of props - you're right, that makes sense - however, this works only if I render it at runtime, it doesn't work when rendering via the next.js integration - is this correct? Otherwise, how do I pass props to a mdx page when it is rendered as part of the integration, such as when Second, you are correct that I am regexing the import away, which is brittle. I'd prefer not to. Come to think of it, if I don't use imports and instead rely on When you say that imports are supported client-side, let me make sure I follow. You're suggesting to allow dynamic imports, somehow, and then the client would fetch the .js file via an import basepath and that would then work? But the file that's being fetched is one that's normally subject to webpack processing (I guess in this case it would be regular JS only.) I haven't tried and am also a bit loathe to try it as it would basically mean that clients directly refer to other files in the codebase, so I'd need to deploy those somehow. But I may try it. Lastly, regarding the suggestion of using MDX on demand or Meanwhile, I've come up with another idea (that seems to work for both scenarios, actually). components = {
...
Config: ObjectComponent(config)
} which is defined like so: // src/ObjectComponent.js
import React from 'react';
export default (obj) => (props) => {
const allprops = Object.keys(props).map((k) => obj[k]).join(" ");
return <>{allprops}</>
} Then I can use |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply. It is indeed difficult for me to choose the right design among the many possible. So far, my rationale for not using Next's on-demand, per request SSR has been to not wanting to have a slow Your advice is sound, though. Perhaps I shouldn't try to make the frameworks do things they weren't designed for. (*) in addition, my hosting provider supports only Apache/Nginx + a single API route. At this point, at least. |
Beta Was this translation helpful? Give feedback.
-
So, I think I got it actually working with your help in the issue regarding wrappers. My wrapper: (props) => {
const router = useRouter();
let mdx = router.asPath;
if (mdx == '/') mdx = '/index'
mdx = `pages${mdx}.mdx`;
return <HotUpdateAbleMDX src={mdx}>{props.children}</HotUpdateAbleMDX>;
}
export function HotUpdateAbleMDX({ children, ...props }) {
return (
<MainTemplate>
<StyledContainer maxWidth="xl">
<HotUpdate {...props}>
{children}
</HotUpdate>
</StyledContainer>
</MainTemplate>
);
} and const components = {
wrapper: ({children}) => (<>{children}</>),
}
export function HotUpdate(props) {
const [mdx, setMdx] = useState("loading updated mdx, please be patient....");
const [newVersion, setNewVersion] = useState(false);
const src = props.src;
useEffect(() => {
let fetchit = async () => {
// fetch new mdx and call setMdx(data);
};
TimeStampManagerFactory().then((timestampemanager) => {
if (!timestampemanager.isUpToDate(src)) {
setNewVersion(true)
fetchit();
}
});
}, [])
if (newVersion) {
const {default: Content} = evaluateSync(mdx, {...mdx_react_provider, ...jsx_runtime})
return (<MDXProvider components={components}>
<Content />
</MDXProvider>);
} else {
return (<>{props.children}</>);
}
} The time stamp manager checks if the API reports a newer modification time for the .mdx on the server. Of course, I can't use imports in the .mdx files but I think I can live with that for now. |
Beta Was this translation helpful? Give feedback.
Hey! You can pass
props
instead: https://mdxjs.com/docs/using-mdx/#props.It sounds like you’re regexing
import
s away, that sounds dangerous and error prone. Imports are supported client side by, I believe, all browsers.For your use case, you should likely do something along the lines of what’s described in the MDX on demand guide.
You should be able to change an MDX file locally, push changes, and Next takes care of deploying your site?