next.js icon indicating copy to clipboard operation
next.js copied to clipboard

CSS order incorrect with the combination of server and client components

Open Netail opened this issue 1 year ago • 4 comments

Link to the code that reproduces this issue

https://github.com/Netail/app-dir-css-order

To Reproduce

  1. yarn dev & open localhost:3000
  2. See 1 yellow and 1 blue component
  3. Uncomment the 'use client' in the navigation-wrapper.tsx
  4. Instead of 1 yellow & 1 blue component it becomes 2 blue ones (Even after a reload & restart)

Current vs. Expected behavior

CSS streaming of client components mess with the order of the css bundle, resulting in being appended later and thus overriding certain server side styling.

In the example it should stay a yellow and blue component instead of 2 blue components when uncommenting 'use client'.

It seem like the page.css overwrites css from the layout.css due to importing order issues. Some classes which get streamed to page.css are already in layout.css, maybe a check could be built in if they were already present in the layout.css bundle?

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.8.1
  npm: 10.5.0
  Yarn: 1.22.22
  pnpm: N/A
Relevant Packages:
  next: 14.3.0-canary.45 // Latest available version is detected (14.3.0-canary.45).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Webpack

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

Netail avatar May 07 '24 20:05 Netail

cc: @sokra as you've worked a lot on the CSS bundling part

https://github.com/vercel/next.js/pull/63157 fixed a big issue we had before, but it seems like there's still an issue left regarding the css order when using both client & server components :(

Netail avatar May 07 '24 21:05 Netail

I think this is also related and is a critical bug: https://github.com/vercel/next.js/issues/64773

iamlinkus avatar May 07 '24 23:05 iamlinkus

I think this is also related and is a critical bug: https://github.com/vercel/next.js/issues/64773

Agreed on this being a critical bug, because this has huge impact on css rendering.

Netail avatar May 07 '24 23:05 Netail

A bit more info found here; https://github.com/vercel/next.js/issues/51030#issuecomment-2220507886

Netail avatar Jul 11 '24 08:07 Netail

Is there any more info about this issue? I'm currently on 14.2.0.canary.28 but I'm still having issue CSS ordering issues when using Client components.

peerjollux avatar Nov 19 '24 15:11 peerjollux

@Netail Can confirm this is still broken with next dev (with --turbopack or not)

samcx avatar Feb 01 '25 01:02 samcx

Might know a fix, using the webpack bundle layer in the css modules hash. This way the classnames won't conflict between server & client components

Netail avatar Jun 27 '25 16:06 Netail