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

Component script in MDX gets loaded in different order compared to regular loading #11219

Open
1 task done
sdegroot opened this issue Jun 9, 2024 · 3 comments
Open
1 task done
Labels
needs response Issue needs response from OP pkg: mdx Issues pertaining to `@astrojs/mdx` integration

Comments

@sdegroot
Copy link

sdegroot commented Jun 9, 2024

Astro Info

Astro                    v4.10.1
Node                     v20.11.0
System                   macOS (arm64)
Package Manager          unknown
Output                   hybrid
Adapter                  @astrojs/vercel/serverless
Integrations             @astrojs/tailwind
                         @astrojs/mdx
                         astro-htmx
                         astro-icon
                         @astrojs/alpinejs

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

No response

Describe the Bug

I am loading a date picker component in two ways: directly on pages and through mdx rendering.

Given the following mdx file:

---
title: DatePicker
--- 
import DatePicker from '~/components/ui/stations/DatePicker.astro'
<DatePicker />

Results into the following outputted HTML:

    <script type="module" src="/@id/astro:scripts/page.js"></script>
    <script type="module" src="/src/components/ui/Loading.astro?astro&type=style&index=0&lang.css"></script>
    <script type="module" src="/src/styles/globals.css"></script>
    <script type="module" src="/node_modules/.pnpm/aos@2.3.4/node_modules/aos/dist/aos.css"></script>
    <script type="module" src="/src/components/ui/stations/DatePicker.astro?astro&#38;type=script&#38;index=0&#38;lang.ts"></script>

Whereas including the date picker in any other location (outside of MDX), it results into the following output:

   <script type="module" src="/src/components/ui/stations/DatePicker.astro?astro&type=script&index=0&lang.ts"></script>
    <script type="module" src="/src/components/ui/accordion/AccordionContainer.astro?astro&type=script&index=0&lang.ts"></script>
    <script type="module" src="/src/components/ui/carousel/Carousel.astro?astro&type=script&index=0&lang.ts"></script>
    <script type="module" src="/@vite/client"></script>
    <script type="module" src="/@fs/Users/sdegroot/scm/zoefapp/aha.zoef.app/zoefapp-website/node_modules/.pnpm/astro@4.10.1_@types+node@20.14.2_typescript@5.4.5/node_modules/astro/dist/runtime/client/dev-toolbar/entrypoint.js?v=a4b7bf2e"></script>
    <script>
    window.__astro_dev_toolbar__ = {
        "root": "/Users/sdegroot/scm/zoefapp/aha.zoef.app/zoefapp-website/",
        "version": "4.10.1",
        "debugInfo": "Astro                    v4.10.1\nNode                     v20.11.0\nSystem                   macOS (arm64)\nPackage Manager          npm\nOutput                   hybrid\nAdapter                  @astrojs/vercel/serverless\nIntegrations             @astrojs/tailwind\n                         @astrojs/mdx\n                         astro-htmx\n                         astro-icon\n                         @astrojs/alpinejs\n                         @astrojs/vercel/serverless"
    }
    </script>
    <script type="module" src="/@id/astro:scripts/page.js"></script>
    <script type="module" src="/src/styles/globals.css"></script>
    <script type="module" src="/node_modules/.pnpm/aos@2.3.4/node_modules/aos/dist/aos.css"></script>
    <script type="module" src="/node_modules/.pnpm/flatpickr@4.6.13/node_modules/flatpickr/dist/themes/dark.css"></script>

I get the impression that there is some kind of encoding issue, as you can clearly see a difference between the two script tags:

   <script type="module" src="/src/components/ui/stations/DatePicker.astro?astro&type=script&index=0&lang.ts"></script>

Compared to 

    <script type="module" src="/src/components/ui/stations/DatePicker.astro?astro&#38;type=script&#38;index=0&#38;lang.ts"></script>

Because of this bug, the DatePicker code is loading after the initialization of alpine. Thus resulting into errors.

What's the expected result?

The loading of an Astro component should work exactly the same within MDX and outside of MDX.
I think the problem may originate in the encoding as mentioned before. Not sure if this does anything with the order?

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-nr2wow-eaomiy

When you check out the provided reproducible example, you will find that the root url shows a working date picker and the two links on that page fail for different reasons.

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 Jun 9, 2024
@ascorbic
Copy link
Contributor

Hey. This issue with the missing renderer looks like it's because you're doing let Content = await render(); rather than let { Content } = await render();

For the other part it looks like you're right about the core of the issue: Alpine is loading before the other parts of the page. I'll defer to someone else to look into this a bit more.

@ascorbic ascorbic added the pkg: mdx Issues pertaining to `@astrojs/mdx` integration label Jun 12, 2024
@matthewp
Copy link
Contributor

There's not a styles/global.css in this project. Does this not reflect your real project?

@matthewp
Copy link
Contributor

I think this loading is correct. DatePicker.astro is last because it is dynamically rendered when you do:

let {render} = await getEntry('examples', 'datepicker')
let Content = await render()

All of the other styles are hoisted and known up-front. In general it's not a good idea to have implicit dependencies where you find yourself depending on ordering which can be difficult to reason about.

@matthewp matthewp added needs response Issue needs response from OP and removed needs triage Issue needs to be triaged labels Jul 31, 2024
emmelia12345 added a commit to emmelia12345/astro that referenced this issue Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs response Issue needs response from OP pkg: mdx Issues pertaining to `@astrojs/mdx` integration
Projects
None yet
3 participants