Skip to content

Commit

Permalink
Fix not found css not being preloaded while navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Aug 11, 2023
1 parent 9229f74 commit 1b1ba67
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"@next/swc": "workspace:*",
"@next/third-parties": "workspace:*",
"@opentelemetry/api": "1.4.1",
"@picocss/pico": "1.5.10",
"@svgr/webpack": "5.5.0",
"@swc/cli": "0.1.55",
"@swc/core": "1.3.55",
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ export async function renderToHTMLOrFlight(

const [NotFound, notFoundStyles] = notFound
? await createComponentAndStyles({
shouldPreload: true,
filePath: notFound[1],
getComponent: notFound[0],
injectedCSS: injectedCSSWithCurrentLayout,
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/e2e/app-dir/app-css/app/navigate/404/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { notFound } from 'next/navigation'

export default function page() {
notFound()
}
12 changes: 12 additions & 0 deletions test/e2e/app-dir/app-css/app/navigate/button/not-found-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use client'

import styles from './not-found-button.module.css'
import React from 'react'

export const Button = ({ children, ...props }) => {
return (
<button {...props} className={styles.button}>
{children}
</button>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.button {
border: 1px solid #ccc;
border-radius: 4px;
background: rgb(0, 128, 0);
color: #fff;
padding: 8px 16px;
}
22 changes: 22 additions & 0 deletions test/e2e/app-dir/app-css/app/navigate/not-found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use client'
import { Button } from './button/not-found-button'
import { useRouter } from 'next/navigation'

function NotFound() {
const router = useRouter()
return (
<div>
<h1>404 - Page Not Found</h1>
<Button
id="back"
onClick={() => {
router.push('/navigate')
}}
>
Back
</Button>
</div>
)
}

export default NotFound
20 changes: 20 additions & 0 deletions test/e2e/app-dir/app-css/app/navigate/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client'

import { useRouter } from 'next/navigation'
import { Button as NotFoundButton } from './button/not-found-button'

export default function Page() {
const router = useRouter()
return (
<>
<NotFoundButton
id="nav-button"
onClick={() => {
router.push('/navigate/404')
}}
>
Not Found
</NotFoundButton>
</>
)
}
29 changes: 29 additions & 0 deletions test/e2e/app-dir/app-css/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,35 @@ createNextDescribe(
)
})

it('should load css while navigation between not-found and page', async () => {
const browser = await next.browser('/navigate')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('#nav-button')).backgroundColor`
),
'rgb(0, 128, 0)'
)
await browser.elementByCss('#nav-button').click()
await browser.waitForElementByCss('#back')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('#back')).backgroundColor`
),
'rgb(0, 128, 0)'
)
await browser.elementByCss('#back').click()
await browser.waitForElementByCss('#nav-button')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('#nav-button')).backgroundColor`
),
'rgb(0, 128, 0)'
)
})

it('should include css imported in server not-found.js', async () => {
const browser = await next.browser('/not-found/servercomponent')
await check(
Expand Down

0 comments on commit 1b1ba67

Please sign in to comment.