-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom markdoc tag to include markdoc file does not render react component #10418
Comments
I've got it kind of working by having a wrapper Astro component: https://stackblitz.com/edit/github-mkxshb-fkgde9?file=markdoc.config.mjs%3AL22 However, the pure astro aside seems to break when used together with the react one: https://stackblitz.com/edit/github-mkxshb-l8dfld?file=src%2Fcontent%2Fdocs%2Fintro.mdoc%3AL13 with the same error
|
Oh, I got it working here: https://stackblitz.com/edit/github-mkxshb-l3fnmn?file=src%2Fcontent%2Fdocs%2Fintro.mdoc%3AL7-L9 The trick is to load the component on the page first, then it can be included in a partial. Bug shows itself if you comment out the component on the page and the partial doesn't know about how to render the component: It seems like there's a cache of the custom tag components loaded in |
NOTE: It doesn't actually matter if the component is wrapped in an Astro component or not. If astro can render it on a page, then the included partial can render it (if it is preloaded on the page). Also on the dev server, once it has been loaded on a page, the server knows how to render the component, and deleting it from the page still allows the included partial to render it. This also means that during builds, as long as there is one page that loads the component, then all partials can load it. If not, the build fails with the above |
@bholmesdev what do you think? |
I've tried a bunch of things, and I've settled on a hack that kinda works.
Here is a stackblitz with the redirect change - https://stackblitz.com/edit/github-mkxshb-5aaguk?file=README.md - however, there is an issue with the styles of the aside not showing, which doesn't happen on my project. Considering it's only a dev issue, this hack is ok for now, until there is a way for me to use the astro runtime to render out the unknown nodes during a page render. Ideally, there would be an Astro API that would allow me to convert any instances of |
@taktran Oh boy, that's quite the clever solution! Sorry you have to jump through those redirect hoops but it's good to know the breakage is dev-only. Let me know if I'm understanding your issues correctly:
|
Hi @bholmesdev , thanks for the prompt reply!
That's not really an issue, which is a bit divergent from my original post. From this comment, I found that the issue happens with plain Astro components and React components. Having a wrapper Astro component or not, doesn't make a difference Here is an example of the same situation happening with just react components: https://stackblitz.com/edit/github-mkxshb-vsepmb
There's a bit of nuance here. You can't use a Markdoc tag in a nested partial if that Markdoc tag is a rendered component (Astro or React or presumably any other framework component) and has not been loaded in the Astro dev server runtime before. To load the Markdoc tag in the dev server runtime, you can either load it on the top level page before you include the partial, or go to a page that has the component loaded already, and then go back to the problematic page (my redirect hack). Once the Astro dev server runtime has been primed, the problematic page is no longer a problem, and can render as normal. If you have a fresh dev server and go to the problematic page first, the dev server errors with the So essentially:
One idea is to inject a
The |
Thanks for the thorough explanation @taktran. I'll find some time to look at the Markdoc internals to see why partials are missed by the bundler. I know we have some pre-scanning in the dev server to hoist scripts and styles, so the partial could trip us up. The idea to add a |
Alright, I'm taking a look at your However, I'll admit you get the same issue using |
Alright, I opened a PR to resolve partials internally to avoid the need for a custom <!--src/content/blog/post.mdoc-->
{% partial file="./_diagram.mdoc" /%}
<!--src/content/blog/_diagram.mdoc-->
## Diagram
{% diagramComponent /%} While implementing, I found we need to handle partial rendering at build time instead of creating a transform as you've built here. The PR implementation ensures validation errors point to the right file path, and components styles and scripts are discovered at build-time for React to mount correctly. This means you can't build your own partial tag in userland if you want components to render correctly. Let me know if this is a good compromise for your use case |
Oh wow @bholmesdev , that sounds amazing! 😍 The
I don't think I would need to build my own partial tag given your solution, so I think the compromise works fine. Thank a bunch for the work! 🌟 |
Use native astro markdoc support for `partial` instead. See withastro/astro#10418 and withastro/astro#10649
Use native astro markdoc support for `partial` instead. See withastro/astro#10418 and withastro/astro#10649
Just a follow up on this - after our upgrade to the new astro markdoc integration, I was able to successfully replace my Thanks again for the update @bholmesdev ! ❤️ So satisfying being able to delete code ✨ 😄 |
Woo, so happy to hear @taktran! Reach out with issues anytime. Want to see more Markdoc power users out there 💪 |
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
I'm trying to create a custom markdoc tag to include other markdoc files, however it doesn't seem to work when those included files use markdoc tags that are rendered using react.
I've implemented my
include-markdoc
custom tag using a transform function, based on how markdoc implements partials (docs).It does work with included files that use markdoc tags that are rendered using astro though.
The error I get is:
Which points to https://github.com/withastro/astro/blob/main/packages/integrations/react/server.js#L22
It seems like a
check
function to see whether the node passed in is a legitimate React component or not. From console logging, the `Component passed in is something likeSo my issue is, how would I pass in a react renderer that
@astro/react
understands? Is there some exposed function somewhere I can use to transform the object above into a valid React component?A side issue is that, maybe the
check
function needs to accommodate for this situation and not throw an error? It currently crashes the dev server and fails to build.What's the expected result?
I can write a custom markdoc tag to include other markdoc files which contain React rendered components.
Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-mkxshb-tsleyv
Participation
The text was updated successfully, but these errors were encountered: