withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.59k stars 2.47k forks source link

CSS bundling issue with lazy components #11404

Open patheticGeek opened 4 months ago

patheticGeek commented 4 months ago

Astro Info

Astro                    v4.7.0
Node                     v20.10.0
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/react

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

No response

Describe the Bug

When we have a lazy loaded component, its css is still getting bundled into the page's css

Consider the following component tree: (in stackblitz example)

Now what happens is:

What's the expected result?

When we load the / page, i would expect

My initial goal for doing all this is:

I want to utilize inlineStylesheets: always to have all styles inlined on the page except for the components which are lazy loaded (modals etc).

Before someone shouts this is bad, this is to optimize for fp/fcp & cls. Our pages render from cached data & avg. page size is less than 50kb with styles inlined so is okay to do. This is what we were already doing with nextjs, dont wanna loose on this critial css optimization in migrating to astro.

Link to Minimal Reproducible Example

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

Participation

matthewp commented 4 months ago

This is an architectural issue with how Astro bundling works. All CSS imported on a page gets bundled together. There isn't a way to dynamically add CSS the way that you'd want it to due to streaming.

patheticGeek commented 4 months ago

but the components explicitly behind a lazy boundary shouldn't have their CSS loaded as it would already be in that chunk. or at the very least the CSS shouldn't be loaded twice & duplicated :thinking: that seems like a bug that css is added in both, the page & the lazy chunk

matthewp commented 4 months ago

Does your example show duplication occuring?

patheticGeek commented 4 months ago

~yes.~ checking the example once :thinking: it was for me

patheticGeek commented 4 months ago

the .lazy-loaded class appears in the page css (with any inlineStylesheets value) and is also loaded in the css file which is loaded when LazyLoaded.jsx's chunk is loaded

patheticGeek commented 4 months ago

confirmed, you can build and preview the example i have added, the css is being duplicated in it it appears in the initial