Unverified Commit 2a61253f authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix misordered CSS resources (#48244)

### What?

This PR fixes misordered CSS `<link>` tags. CSS imports in inner layers
(e.g. a page) should always take precedence over outer layers (e.g. root
layout), but currently it's reversed.

### Why?

In layouts we usually define more general styles like globals, resets,
and layout specific things. And in inner layers and pages, things need
to be more detailed and override upper layers if there're any conflicts.

Previously we defined the component segment as

```tsx
<>
  <Component {...props} />
  {assets}
</>
```

which is necessary because of `findDOMNode` - if we put `assets` before
the component, we can't find the correct scrolling DOM and position for
that layer.

However, with `assets` being the last Float will receive the reversed
order of resources.

### How?

I changed the `createComponentTree` function to return a `Component` and
`assets` pair, so in the Layout Router they're no longer passed to the
scroll wrapper altogether but separately.

Closes NEXT-983
Fixes #47585, fixes #46347.

fix NEXT-656

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 3ac76585
......@@ -453,6 +453,7 @@ export default function OuterLayoutRouter({
notFound,
notFoundStyles,
asNotFound,
styles,
}: {
parallelRouterKey: string
segmentPath: FlightSegmentPath
......@@ -467,6 +468,7 @@ export default function OuterLayoutRouter({
notFound: React.ReactNode | undefined
notFoundStyles: React.ReactNode | undefined
asNotFound?: boolean
styles?: React.ReactNode
}) {
const context = useContext(LayoutRouterContext)
if (!context) {
......@@ -501,6 +503,7 @@ export default function OuterLayoutRouter({
return (
<>
{styles}
{preservedSegments.map((preservedSegment) => {
const isChildPropSegment = matchSegment(
preservedSegment,
......
@layer default;
@layer default {
h1 {
color: black;
}
}
@layer default {
input[type='text'] {
padding: 0.5rem;
}
h1 {
color: red;
}
}
'use client'
import './input.css'
export default function InputText() {
return <input />
}
import './index.css'
export default function Layout({ children }) {
return <div>{children}</div>
}
import InputText from './input'
export default function Home() {
return (
<div>
<h1>Hello</h1>
<InputText />
</div>
)
}
......@@ -239,6 +239,17 @@ createNextDescribe(
})
})
describe('css ordering', () => {
it('should have inner layers take precedence over outer layers', async () => {
const browser = await next.browser('/ordering')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
})
})
if (isDev) {
describe('multiple entries', () => {
it('should only inject the same style once if used by different layers', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment