Skip to content

Commit 9834ab7

Browse files
authored
Fix not found css not being preloaded while navigation (#53906)
### Problem One style of `not-found` has `precendence` property with "undefined" value, which can't be handled by React Float, then during navigation the style could not load properly, lead to the style missing in issue #53210. ### Solution Always enable `precendence` for all links, so all the css styles of page and convention components can be hoist by react properly. Float will decide which one should be handled. Previously this change only applies to template, actually we can apply it to all components so that they can all be handled properly especially during client navigation. Related react change: facebook/react#27265 Fixes #53210
1 parent 06be3c6 commit 9834ab7

File tree

11 files changed

+147
-13
lines changed

11 files changed

+147
-13
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"@next/swc": "workspace:*",
8383
"@next/third-parties": "workspace:*",
8484
"@opentelemetry/api": "1.4.1",
85+
"@picocss/pico": "1.5.10",
8586
"@svgr/webpack": "5.5.0",
8687
"@swc/cli": "0.1.55",
8788
"@swc/core": "1.3.55",

packages/next/src/server/app-render/app-render.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,10 @@ export async function renderToHTMLOrFlight(
401401
const createComponentAndStyles = async ({
402402
filePath,
403403
getComponent,
404-
shouldPreload,
405404
injectedCSS,
406405
}: {
407406
filePath: string
408407
getComponent: () => any
409-
shouldPreload?: boolean
410408
injectedCSS: Set<string>
411409
}): Promise<any> => {
412410
const cssHrefs = getCssInlinedLinkTags(
@@ -433,11 +431,8 @@ export async function renderToHTMLOrFlight(
433431
// During HMR, it's critical to use different `precedence` values
434432
// for different stylesheets, so their order will be kept.
435433
// https://github.com/facebook/react/pull/25060
436-
const precedence = shouldPreload
437-
? process.env.NODE_ENV === 'development'
438-
? 'next_' + href
439-
: 'next'
440-
: undefined
434+
const precedence =
435+
process.env.NODE_ENV === 'development' ? 'next_' + href : 'next'
441436

442437
return (
443438
<link
@@ -614,7 +609,6 @@ export async function renderToHTMLOrFlight(
614609
? await createComponentAndStyles({
615610
filePath: template[1],
616611
getComponent: template[0],
617-
shouldPreload: true,
618612
injectedCSS: injectedCSSWithCurrentLayout,
619613
})
620614
: [React.Fragment]

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/app-dir/app-css/index.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ createNextDescribe(
175175

176176
describe('special entries', () => {
177177
it('should include css imported in loading.js', async () => {
178-
const html = await next.render('/loading-bug/hi')
179-
// The link tag should be included together with loading
180-
expect(html).toMatch(
181-
/<link rel="stylesheet" href="(.+)\.css(\?v=\d+)?"\/><h2>Loading...<\/h2>/
182-
)
178+
const $ = await next.render$('/loading-bug/hi')
179+
// The link tag should be hoist into head with precedence properties
180+
expect($('head link[data-precedence]').length).toBe(2)
181+
182+
expect($('body h2').text()).toBe('Loading...')
183183
})
184184

185185
it('should include css imported in client template.js', async () => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Layout({ children }) {
2+
return (
3+
<html>
4+
<body>{children}</body>
5+
</html>
6+
)
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use client'
2+
3+
import { Button } from '../components/button/button'
4+
import { useRouter } from 'next/navigation'
5+
6+
function NotFound() {
7+
const router = useRouter()
8+
return (
9+
<div>
10+
<h1>404 - Page Not Found</h1>
11+
<Button
12+
id="go-to-index"
13+
onClick={() => {
14+
router.push('/')
15+
}}
16+
>
17+
Home
18+
</Button>
19+
</div>
20+
)
21+
}
22+
23+
export default NotFound
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use client'
2+
3+
import { useRouter } from 'next/navigation'
4+
import { Button } from '../components/button/button'
5+
6+
export default function Page() {
7+
const router = useRouter()
8+
return (
9+
<>
10+
<Button
11+
id="go-to-404"
12+
onClick={() => {
13+
router.push('/404')
14+
}}
15+
>
16+
Not Found
17+
</Button>
18+
</>
19+
)
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use client'
2+
3+
import styles from './button.module.css'
4+
import React from 'react'
5+
6+
export const Button = ({ children, ...props }) => {
7+
return (
8+
<button {...props} className={styles.button}>
9+
{children}
10+
</button>
11+
)
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.button {
2+
border: 1px solid #ccc;
3+
border-radius: 4px;
4+
background: rgb(0, 128, 0);
5+
color: #fff;
6+
padding: 8px 16px;
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use client'
2+
3+
import { Button } from './button/button'
4+
import { useRouter } from 'next/navigation'
5+
6+
export default function Navigate404Section() {
7+
const router = useRouter()
8+
return (
9+
<p>
10+
<Button
11+
id="go-to-navigate-404"
12+
onClick={() => {
13+
router.push('/navigate-404')
14+
}}
15+
>
16+
Go to /navigate-404
17+
</Button>
18+
</p>
19+
)
20+
}

0 commit comments

Comments
 (0)