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

CSS Styles from unrelated layouts are merged if they have one CSS file in common

Open onigoetz opened this issue 1 year ago • 10 comments

Link to the code that reproduces this issue

https://github.com/onigoetz/nextjs-css-issue-repro

To Reproduce

  1. build the app and start it (not reproducible in dev mode)
  2. go to http://localhost:3000/
  3. you should see a green background as defined in its stylesheet

Current vs. Expected behavior

The background should be green but is red.

The styles from the subpage http://localhost:3000/leakingstyle are applied to http://localhost:3000/ but they have no layout in common and should not. It seems that because they have another CSS file in common they are merged

Another page next to it that has no css file in common does not have this issue : http://localhost:3000/otherstyle

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Fri Jun 2 00:45:15 UTC 2023
  Available memory (MB): 17991
  Available CPU cores: 6
Binaries:
  Node: 20.11.1
  npm: 10.2.4
  Yarn: 1.22.21
  pnpm: 8.14.1
Relevant Packages:
  next: 14.2.2
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
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), Vercel (Deployed)

Additional context

I tested multiple versions:

  • 14.1.4 : OK
  • 14.2.0-canary.27: OK
  • 14.2.0-canary.28: BROKEN
  • 14.2.0 : BROKEN
  • 14.2.1 : BROKEN
  • 14.2.2 : BROKEN
  • 14.3.0-canary.11: BROKEN

Apparently the issue has been introduced in https://github.com/vercel/next.js/releases/tag/v14.2.0-canary.28

I would bet that the issue was introduced in this PR : https://github.com/vercel/next.js/pull/63157

onigoetz avatar Apr 19 '24 10:04 onigoetz

+1

Experiencing the exact same, super annoying. Happens only on build though.

iamlinkus avatar Apr 24 '24 16:04 iamlinkus

+1

This issue is directly responsible for a 5-10% performance drop on our SEO optimised pages, in which the Lighthouse "eliminate render blocking resources" diagnostic suggests a savings of 1080ms by removing unused CSS, whereas with 14.1.4 it was around 250ms.

astateful avatar Apr 27 '24 14:04 astateful

Same here, happens only on build. It currently blocks us from upgrading next.js version from 14.1.4 to 14.2.3

alexanderchabo avatar Apr 29 '24 19:04 alexanderchabo

Unfortunately running in to the same issue where my frontend styles are merged into the backend.

mvdve avatar May 02 '24 19:05 mvdve

This actually also happens when there’s no CSS files in common. A single global css (non module) import in one parallel route layout results in those styles being imported in other parallel routes.

iamlinkus avatar May 07 '24 23:05 iamlinkus

This is super annoying and makes it impossible to use next.js with the payloadcms 3.0, unless you have no global styles (which is rarely the case). Is there any update on this from the next team?

iamlinkus avatar May 30 '24 10:05 iamlinkus

@onigoetz Thank you for sharing. I can confirm this is an issue we're there is different behavior happening from next dev / next dev --turbo and next build.

We are taking a look at several CSS issues at the moment and hopefully we can land several fixes at once! Will be sharing updates and testing these :repro:s as we get them.

samcx avatar Jun 03 '24 19:06 samcx

OMG I am not the only one. I just updated to the lastest version (14.2.4). I almost pulled the hair out of my head. xD

jricaldi avatar Jun 12 '24 04:06 jricaldi

Any updates on this issue? Has anyone found a canary version that doesn't break the merged css files?

Edit by maintainer bot: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

balatD avatar Jun 27 '24 07:06 balatD

Any updates on this issue?

KnotScientific avatar Jun 27 '24 20:06 KnotScientific

No updates yet, but we are indeed prioritizing this issue!

@balatD The CSS merging works as expected versions prior to v14.2.0-canary.28 (just tested on v14.2.0-canary.27).

samcx avatar Jul 01 '24 13:07 samcx

In lieu of a +1 I will just say that my project is stuck on 14.1.x because of this issue. We are using Sass + CSS Modules, with a single global css file that contains resets and utility classes (i.e. mobile-only).

timhettler avatar Jul 01 '24 21:07 timhettler

Hi everyone—

Let us know if canary v15.0.0-canary.56 or higher fixes your issue—we recently shipped a bug fix that changes how we merge global css → https://github.com/vercel/next.js/pull/67373.

@onigoetz I can no longer reproduce the issue!

CleanShot 2024-07-08 at 16 17 08@2x

CleanShot 2024-07-08 at 16 16 12@2x

As described in the :pr:, the global css file is now a separate chunk (2nd screenshot).

samcx avatar Jul 08 '24 13:07 samcx

@samcx Appreciate that there is progress on this issue - Will these fixes be made in the Next 14.x?

timhettler avatar Jul 18 '24 21:07 timhettler

@timhettler It's been backported to 14.2.5!

samcx avatar Jul 19 '24 09:07 samcx

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

github-actions[bot] avatar Aug 02 '24 12:08 github-actions[bot]