Skip to content

lazy-loaded styles get included even before the code is executed #9495

Description

@mayank99

Astro Info

Astro                    v4.0.7
Node                     v18.18.0
System                   Linux (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

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

No response

Describe the Bug

lazy loading styles using dynamic import() like this does not work:

async function loadCss() {
	await import("../styles.css");
}

the styles will get loaded even before the function is called.

same scenario works correctly in vite: https://stackblitz.com/edit/vitejs-vite-u5qhnz?file=index.html,styles.css&terminal=dev


possibly related issue: #6328

What's the expected result?

styles should only be imported after the code is actually called.

one important detail: in my case, the import() is only used in a client-side script, so astro should never inline it, only bundle it.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-xmc34j

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P3: minor bugAn edge case that only affects very specific usage (priority)feat: stylingRelated to styles (scope)requires refactorBug, may take longer as fixing either requires refactors, breaking changes, or considering tradeoffs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions