Skip to content
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

MDX Content component imported via Astro.glob initially renders child components unstyled #12004

Closed
1 task
alexjcook opened this issue Sep 16, 2024 · 5 comments
Closed
1 task
Assignees
Labels
- P2: has workaround Bug, but has workaround (priority)

Comments

@alexjcook
Copy link

Astro Info

Astro                    v4.15.6
Node                     v21.6.1
System                   Windows (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/mdx

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When using Astro.glob() to import mdx file(s), the <Content> component fails to render styles of any children components on initial page load of dev server. If the page is refreshed or a browser tab was already open to the dev server url, the child components are rendered with styles correctly.

Build command renders components correctly.

What's the expected result?

Content component should display correctly on initial page load of dev server

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-1acyzy?file=src%2Fpages%2Findex.astro

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Sep 16, 2024
@matthewp matthewp added - P2: has workaround Bug, but has workaround (priority) and removed needs triage Issue needs to be triaged labels Sep 16, 2024
@matthewp
Copy link
Contributor

Astro.glob has been deprecated and will be removed in a future version of Astro. I suggest starting to use Content Collections instead: https://docs.astro.build/en/tutorials/add-content-collections/

@alexjcook
Copy link
Author

Thanks, I wasn't aware of Astro.glob being deprecated.

Will the Content component from import.meta.glob similarly be deprecated? I was able to reproduce the same inconsistent behaviour:
https://stackblitz.com/edit/github-1acyzy-43rpru?file=src%2Fpages%2Findex.astro

And thanks for the tip about Content Collections, I'll take a look.

@bluwy bluwy self-assigned this Sep 19, 2024
@bluwy
Copy link
Member

bluwy commented Sep 19, 2024

The issue seems to be that with Astro.glob or import.meta.glob, you're dynamically importing the MDX files, so we're unable to analyze the CSS used ahead of time because Vite will only keep track of those imported MDX files when the <Content /> are being called.

When the <Content /> is called, it'll already be too late (during streaming) since we've already rendered the head and stuff when we reach at the point of the <Content /> renders. The styles need to be hoisted to the head. The next refresh works because the previous run already keeps track of the imported MDX file so it could use it to hoist the styles to the head directly before needing to render the <Content />.

So I think this is more of a limitation of the current rendering. You can workaround this with import.meta.glob(..., { eager: true }) instead so that it's imported directly (instead of dynamic import) and we can analyze because we know it'll be used. Using content collections or content layer should also avoid this issue since we'd have more control of the page rendering I believe.

@ascorbic
Copy link
Contributor

ascorbic commented Oct 3, 2024

@bluwy Is this something we're ever likely to be able to fix, or can this be closed?

@bluwy
Copy link
Member

bluwy commented Oct 7, 2024

Yeah I think we can close this. I don't think there's an easy fix in the long run anyways, and using content collections or content layer would be better (for other reasons too) for now.

@bluwy bluwy closed this as completed Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P2: has workaround Bug, but has workaround (priority)
Projects
None yet
Development

No branches or pull requests

4 participants