Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not-found rendering in production with edge #53687

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
{ req, res, pathname, renderOpts: opts }: RequestContext,
{ components, query }: FindComponentsResult
): Promise<ResponsePayload | null> {
const is404Page = pathname === '/404'
const is404Page =
// For edge runtime 404 page, /_not-found needs to be treated as 404 page
(process.env.NEXT_RUNTIME === 'edge' && pathname === '/_not-found') ||
pathname === '/404'
const is500Page = pathname === '/500'
const isAppPath = components.isAppPath
const hasServerProps = !!components.getServerSideProps
Expand Down Expand Up @@ -1920,7 +1923,12 @@ export default abstract class Server<ServerOptions extends Options = Options> {
(req as NodeNextRequest).originalRequest ?? (req as WebNextRequest),
(res as NodeNextResponse).originalResponse ??
(res as WebNextResponse),
{ page: pathname, params: opts.params, query, renderOpts }
{
page: is404Page ? '/404' : pathname,
params: opts.params,
query,
renderOpts,
}
)
} else {
// If we didn't match a page, we should fallback to using the legacy
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
)
}

// For edge runtime if the pathname hit as /_not-found entrypoint,
// override the pathname to /404 for rendering
if (pathname === (renderOpts.dev ? '/not-found' : '/_not-found')) {
pathname = '/404'
}
return renderToHTML(
req as any,
res as any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'

createNextDescribe(
'app dir - not-found',
'app dir - not-found - basic',
{
files: __dirname,
skipDeployment: true,
Expand Down Expand Up @@ -32,11 +32,6 @@ createNextDescribe(
expect(await browser.elementByCss('#layout-nav').text()).toBe('Navbar')
})

it('should allow to have a valid /not-found route', async () => {
const html = await next.render('/not-found')
expect(html).toContain("I'm still a valid page")
})

it('should match dynamic route not-found boundary correctly', async () => {
const $dynamic = await next.render$('/dynamic')
const $dynamicId = await next.render$('/dynamic/123')
Expand All @@ -61,21 +56,17 @@ createNextDescribe(
}, 'success')
})

// TODO: investigate isEdge case
if (!isEdge) {
it('should render the 404 page when the file is removed, and restore the page when re-added', async () => {
const browser = await next.browser('/')
await check(() => browser.elementByCss('h1').text(), 'My page')
await next.renameFile('./app/page.js', './app/foo.js')
await check(
() => browser.elementByCss('h1').text(),
'This Is The Not Found Page'
)
// TODO: investigate flakey behavior
// await next.renameFile('./app/foo.js', './app/page.js')
// await check(() => browser.elementByCss('h1').text(), 'My page')
})
}
it('should render the 404 page when the file is removed, and restore the page when re-added', async () => {
const browser = await next.browser('/')
await check(() => browser.elementByCss('h1').text(), 'My page')
await next.renameFile('./app/page.js', './app/foo.js')
await check(
() => browser.elementByCss('h1').text(),
'This Is The Not Found Page'
)
await next.renameFile('./app/foo.js', './app/page.js')
await check(() => browser.elementByCss('h1').text(), 'My page')
})
}

if (!isNextDev && !isEdge) {
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/app-dir/not-found/conflict-route/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default function Layout({ children }) {
return (
<html>
<head />
<body>
<header>
<nav id="layout-nav">Navbar</nav>
</header>
<main>{children}</main>
<footer>
<p id="layout-footer">Footer</p>
</footer>
</body>
</html>
)
}
11 changes: 11 additions & 0 deletions test/e2e/app-dir/not-found/conflict-route/app/not-found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function NotFound() {
return (
<>
<h1>This Is The Not Found Page</h1>

<div id="timestamp">{Date.now()}</div>
</>
)
}

NotFound.displayName = 'NotFound'
3 changes: 3 additions & 0 deletions test/e2e/app-dir/not-found/conflict-route/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <h1>My page</h1>
}
49 changes: 49 additions & 0 deletions test/e2e/app-dir/not-found/conflict-route/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { createNextDescribe } from 'e2e-utils'

createNextDescribe(
'app dir - not-found - conflict route',
{
files: __dirname,
skipDeployment: true,
},
({ next }) => {
const runTests = () => {
it('should use the not-found page for non-matching routes', async () => {
const browser = await next.browser('/random-content')
expect(await browser.elementByCss('h1').text()).toContain(
'This Is The Not Found Page'
)
// should contain root layout content
expect(await browser.elementByCss('#layout-nav').text()).toBe('Navbar')
})

it('should allow to have a valid /not-found route', async () => {
const html = await next.render('/not-found')
expect(html).toContain("I'm still a valid page")
})
}

describe('with default runtime', () => {
runTests()
})

describe('with runtime = edge', () => {
let originalLayout = ''

beforeAll(async () => {
await next.stop()
originalLayout = await next.readFile('app/layout.js')
await next.patchFile(
'app/layout.js',
`export const runtime = 'edge'\n${originalLayout}`
)
await next.start()
})
afterAll(async () => {
await next.patchFile('app/layout.js', originalLayout)
})

runTests()
})
}
)
1 change: 0 additions & 1 deletion test/e2e/app-dir/not-found/next.config.js

This file was deleted.

Loading