-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix CSS duplication related problems (#50406)
This PR fixes a couple of categories of CSS issues in App Router, that come from the same root cause. ### 1. Duplicated styles being loaded in different layers This issue has been described in vanilla-extract-css/vanilla-extract#1088 (comment). If a CSS module (or a global CSS) is referenced in multiple layers (e.g. a layout and a page), it will be bundled into multiple CSS assets because each layer is considered as a separate entry. <img width="1141" alt="CleanShot-2023-05-26-GoB9Rhcs@2x" src="https://github.com/vercel/next.js/assets/3676859/8e0f5346-ee64-4553-950a-7fb44f769efc"> As explained in that issue, we have to bundle all CSS modules into one chunk to avoid a big number of requests. ### 2. CSS ordering issues (conflicts) This is likely causing #48120. When the layer-based bundling and ordering logic applies to CSS, it can potentially cause non-deterministic order. In this example, button A in the layout should be in blue. However when button B is imported by the page, button A becomes red. This is an inconsistent experience and can be hard to debug and fix. <img width="1090" alt="CleanShot-2023-05-26-Ar4MN5rP@2x" src="https://github.com/vercel/next.js/assets/3676859/4328d5d7-23af-4c42-bedf-30f8f062d96a">
- Loading branch information
Showing
9 changed files
with
165 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
test/e2e/app-dir/app-css/app/css/css-conflict-layers/blue-button.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Button } from './button' | ||
import styles from './blue-button.module.css' | ||
|
||
export function BlueButton() { | ||
return <Button className={'btn-blue ' + styles['blue-button']}>Button</Button> | ||
} |
4 changes: 4 additions & 0 deletions
4
test/e2e/app-dir/app-css/app/css/css-conflict-layers/blue-button.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.blue-button { | ||
color: white; | ||
background: blue; | ||
} |
5 changes: 5 additions & 0 deletions
5
test/e2e/app-dir/app-css/app/css/css-conflict-layers/button.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import styles from './button.module.css' | ||
|
||
export function Button({ className = '' }) { | ||
return <div className={'btn ' + styles.button + ' ' + className}>Button</div> | ||
} |
5 changes: 5 additions & 0 deletions
5
test/e2e/app-dir/app-css/app/css/css-conflict-layers/button.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.button { | ||
display: inline-block; | ||
border: 1px solid black; | ||
background: white; | ||
} |
12 changes: 12 additions & 0 deletions
12
test/e2e/app-dir/app-css/app/css/css-conflict-layers/layout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { BlueButton } from './blue-button' | ||
|
||
export default function ServerLayout({ children }) { | ||
return ( | ||
<> | ||
<div> | ||
Blue Button: <BlueButton /> | ||
</div> | ||
{children} | ||
</> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/app-css/app/css/css-conflict-layers/page.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
font-size: large; | ||
} |
13 changes: 13 additions & 0 deletions
13
test/e2e/app-dir/app-css/app/css/css-conflict-layers/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import './page.css' | ||
|
||
import { Button } from './button' | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<div> | ||
Button: <Button /> | ||
</div> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters