Skip to content

Commit 1b1ba67

Browse files
committed
Fix not found css not being preloaded while navigation
1 parent 9229f74 commit 1b1ba67

File tree

9 files changed

+104
-0
lines changed

9 files changed

+104
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"@next/swc": "workspace:*",
8181
"@next/third-parties": "workspace:*",
8282
"@opentelemetry/api": "1.4.1",
83+
"@picocss/pico": "1.5.10",
8384
"@svgr/webpack": "5.5.0",
8485
"@swc/cli": "0.1.55",
8586
"@swc/core": "1.3.55",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ export async function renderToHTMLOrFlight(
694694

695695
const [NotFound, notFoundStyles] = notFound
696696
? await createComponentAndStyles({
697+
shouldPreload: true,
697698
filePath: notFound[1],
698699
getComponent: notFound[0],
699700
injectedCSS: injectedCSSWithCurrentLayout,

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.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { notFound } from 'next/navigation'
2+
3+
export default function page() {
4+
notFound()
5+
}
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 './not-found-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use client'
2+
import { Button } from './button/not-found-button'
3+
import { useRouter } from 'next/navigation'
4+
5+
function NotFound() {
6+
const router = useRouter()
7+
return (
8+
<div>
9+
<h1>404 - Page Not Found</h1>
10+
<Button
11+
id="back"
12+
onClick={() => {
13+
router.push('/navigate')
14+
}}
15+
>
16+
Back
17+
</Button>
18+
</div>
19+
)
20+
}
21+
22+
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 as NotFoundButton } from './button/not-found-button'
5+
6+
export default function Page() {
7+
const router = useRouter()
8+
return (
9+
<>
10+
<NotFoundButton
11+
id="nav-button"
12+
onClick={() => {
13+
router.push('/navigate/404')
14+
}}
15+
>
16+
Not Found
17+
</NotFoundButton>
18+
</>
19+
)
20+
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,35 @@ createNextDescribe(
215215
)
216216
})
217217

218+
it('should load css while navigation between not-found and page', async () => {
219+
const browser = await next.browser('/navigate')
220+
await check(
221+
async () =>
222+
await browser.eval(
223+
`window.getComputedStyle(document.querySelector('#nav-button')).backgroundColor`
224+
),
225+
'rgb(0, 128, 0)'
226+
)
227+
await browser.elementByCss('#nav-button').click()
228+
await browser.waitForElementByCss('#back')
229+
await check(
230+
async () =>
231+
await browser.eval(
232+
`window.getComputedStyle(document.querySelector('#back')).backgroundColor`
233+
),
234+
'rgb(0, 128, 0)'
235+
)
236+
await browser.elementByCss('#back').click()
237+
await browser.waitForElementByCss('#nav-button')
238+
await check(
239+
async () =>
240+
await browser.eval(
241+
`window.getComputedStyle(document.querySelector('#nav-button')).backgroundColor`
242+
),
243+
'rgb(0, 128, 0)'
244+
)
245+
})
246+
218247
it('should include css imported in server not-found.js', async () => {
219248
const browser = await next.browser('/not-found/servercomponent')
220249
await check(

0 commit comments

Comments
 (0)