-
Link to the code that reproduces this issuehttps://github.com/Obvio/next-css-modules To ReproduceThis issue is similar to #64773. To Reproduce
Visit the site in https://next-css-modules.vercel.app/ looking at the console we see one that one of the css files includes all the "shapes" of .h1 even-though only one of them is called/used on the page. I have 2 pages that share the same component, each hast its own css module and the shared component also has its own css module. When the app is built and deployed in vercel (doesn't happen in localhost) the bundle generates a css file which includes all the places said shared component is called from with their page css. This seem to go against the site's performance and in bigger scale causes pagespeed and lighthouse to not like (and punish) for the increased bundle size. Current vs. Expected behaviorCurrently the generated css files in the bundle "merge" all into one file. Provide environment informationOperating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:00 PDT 2024; root:xnu-10063.141.2~1/RELEASE_X86_64
Available memory (MB): 32768
Available CPU cores: 12
Binaries:
Node: 21.1.0
npm: 10.2.0
Yarn: 1.22.19
pnpm: 7.26.2
Relevant Packages:
next: 15.0.0-canary.156 // Latest available version is detected (15.0.0-canary.156).
eslint-config-next: 15.0.0-canary.156
react: 19.0.0-rc-206df66e-20240912
react-dom: 19.0.0-rc-206df66e-20240912
typescript: 5.6.2
Next.js Config:
output: N/A Which area(s) are affected? (Select all that apply)Not sure Which stage(s) are affected? (Select all that apply)next build (local) Additional context14.1.4 : OK |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 23 replies
-
@Obvio The behavior was changed here → #63157. This is the expected behavior.
The other side of this to consider is that more requests will also punish site performance. https://github.com/vercel/next.js/blob/canary/packages/next/src/build/webpack/plugins/css-chunking-plugin.ts → I believe essentially what we do is we merge until a chunk is too large, then we split it is as needed. |
Beta Was this translation helpful? Give feedback.
-
Hey y'all! I recently migrated my blog to Next 15, and I believe this behaviour is causing a significant performance issue. Most pages have 20-40kb of unused CSS being loaded. My homepage, for example, loads 38kb of CSS but only uses 4kb: This is a problem since the CSS blocks rendering. It means the user spends longer staring at a blank white screen: According to Lighthouse, the FCP has been pushed back by ~500ms on mobile, and I believe this change is responsible for the majority of that time (though I could be wrong). So it seems to me like a pretty significant regression. I understand @sokra’s point, that out-of-order CSS is problematic and a significant challenge given the new architecture, but if I had to choose between the occasional selector issue and significantly worse initial load experience for all users, I would absolutely pick the former. I believe I have seen the out-of-order CSS issue in development, but it's rare, and easy enough to fix (I solve it by doubling up the selector when I see it happening and know one style should always be prioritized over the other). Hopefully this can be made into an config option. Otherwise, I think this fact needs to be published much more prominently, as it's important information for anyone trying to decide whether to migrate to App Router or not. |
Beta Was this translation helpful? Give feedback.
-
Related: #71474 |
Beta Was this translation helpful? Give feedback.
-
Hey everyone! We recently shipped an experimental flag to disable the CSS Chunking Algorithm altogether → #73286, which may help with this issue (does have tradeoffs to doing this). We also recently shipped an experimental flag to inline CSS → https://nextjs.org/docs/canary/app/api-reference/config/next-config-js/inlineCss. More improvements to come with this flag, but this can definitely improve Lighthouse scores! |
Beta Was this translation helpful? Give feedback.
Even with CSS Modules order matters in same cases. E. g. when applying styles from two different CSS modules to the same element:
This often happens with components that allow to pass a
className
prop.Agree. We usually optimize with the focus on initial page load, but here we have to make trade-offs for correctness.
There is an alternative approach for CSS we experimented with for Turbopack…