Open Saeris opened 2 years ago
Thanks a lot for opening this issue and providing a minimal reproduction - very helpful!
I can confirm that component HMR (as well as cache invalidation) indeed doesn't work when importing components from MDX files directly. However (at least as a workaround for now), it does already work as expected in case you're importing the used React components like in the screenshot below:
This is closely related to #247
Is HMR "supposed" to work at all? For me it doesn't seem to work properly for regular mdx-files either, no components or imports, just regular markdown with frontmatter and some plain html elements. 🤔
It does pick up on new files I add, but when I change existing files, I can't see the changes no matter how hard I reload the browser. I have to shutdown the nextjs dev server and restart it, then the change appears on the next refresh. 🤷♂️
I'm on Windows, if that's related. I get a "Warning: Contentlayer might not work as expected on Windows" in my console, but don't know what "expected" is related to here...
@Svish When using imported components in MDX files I've found that it only refreshes after re-saving the MDX file itself.
@city17 It's the MDX file itself I'm editing, and it doesn't even use any imported components at all.
Put together an example here: https://github.com/Saeris/contentlayer-mdx-example
Two things I've observed when trying to import and use components within MDX documents:
yarn dev
then go tosrc/components/ExampleComponent.tsx
and uncomment line 18, this should cause a reload because the component has been changed. I used inline styles here to make it easy to inspect the HTML to confirm the embedded component doesn't update.posts/mdx-component-example.mdx
has any changes (technically this is true, the content of this file is unchanged), so it serves the previously compiled version from the cache. You have to manually purge the cache and generated files in order for any changes to imported components to be reflected in the app.Effectively this makes authoring mdx content quite a chore and is far from intuitive.
Additionally, if you want to import components that themselves have dependencies (in
ExampleComponent
, I'm importingButton
), you can't set"target": "es5",
inside oftsconfig.json
such as in the official nextjs example, as you'll get a compilation error:It would be very helpful if there was an official example of how to use custom components in MDX with Contentlayer. Right now there is little if anything to go off of in the documentation.