Unverified Commit 02197bc3 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix status code for /_not-found route (#64058)

### What

Fix the status code in static generation metadata for `/_not-found`
route, aligning it as 404 for both dev and build

### Why
`/_not-found` route should still return 404 code as it's reserved as
default not found route

Closes NEXT-3001
parent 00d9c581
...@@ -40,9 +40,11 @@ export async function exportAppPage( ...@@ -40,9 +40,11 @@ export async function exportAppPage(
isDynamicError: boolean, isDynamicError: boolean,
fileWriter: FileWriter fileWriter: FileWriter
): Promise<ExportRouteResult> { ): Promise<ExportRouteResult> {
let isDefaultNotFound = false
// If the page is `/_not-found`, then we should update the page to be `/404`. // If the page is `/_not-found`, then we should update the page to be `/404`.
// UNDERSCORE_NOT_FOUND_ROUTE value used here, however we don't want to import it here as it causes constants to be inlined which we don't want here. // UNDERSCORE_NOT_FOUND_ROUTE value used here, however we don't want to import it here as it causes constants to be inlined which we don't want here.
if (page === '/_not-found') { if (page === '/_not-found/page') {
isDefaultNotFound = true
pathname = '/404' pathname = '/404'
} }
...@@ -137,8 +139,11 @@ export async function exportAppPage( ...@@ -137,8 +139,11 @@ export async function exportAppPage(
? res.statusCode ? res.statusCode
: undefined : undefined
// If it's parallel route the status from mock response is 404 if (isDefaultNotFound) {
if (isNonSuccessfulStatusCode && !isParallelRoute) { // Override the default /_not-found page status code to 404
status = 404
} else if (isNonSuccessfulStatusCode && !isParallelRoute) {
// If it's parallel route the status from mock response is 404
status = res.statusCode status = res.statusCode
} }
......
...@@ -1747,11 +1747,10 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1747,11 +1747,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
{ req, res, pathname, renderOpts: opts }: RequestContext, { req, res, pathname, renderOpts: opts }: RequestContext,
{ components, query }: FindComponentsResult { components, query }: FindComponentsResult
): Promise<ResponsePayload | null> { ): Promise<ResponsePayload | null> {
const is404Page = if (pathname === UNDERSCORE_NOT_FOUND_ROUTE) {
// For edge runtime 404 page, /_not-found needs to be treated as 404 page pathname = '/404'
(process.env.NEXT_RUNTIME === 'edge' && }
pathname === UNDERSCORE_NOT_FOUND_ROUTE) || const is404Page = pathname === '/404'
pathname === '/404'
// Strip the internal headers. // Strip the internal headers.
this.stripInternalHeaders(req) this.stripInternalHeaders(req)
......
'use client' 'use client'
import React, { useState } from 'react' import { useState, Suspense } from 'react'
import { notFound } from 'next/navigation' import { notFound } from 'next/navigation'
import NotFoundTrigger from './not-found-trigger' import NotFoundTrigger from './not-found-trigger'
...@@ -13,9 +13,9 @@ export default function Root({ children }) { ...@@ -13,9 +13,9 @@ export default function Root({ children }) {
return ( return (
<html className="root-layout-html"> <html className="root-layout-html">
<body> <body>
<React.Suspense fallback={<div>Loading...</div>}> <Suspense fallback={<div>Loading...</div>}>
<NotFoundTrigger /> <NotFoundTrigger />
</React.Suspense> </Suspense>
<button id="trigger-not-found" onClick={() => setClicked(true)}> <button id="trigger-not-found" onClick={() => setClicked(true)}>
Click to not found Click to not found
</button> </button>
......
...@@ -33,6 +33,11 @@ createNextDescribe( ...@@ -33,6 +33,11 @@ createNextDescribe(
) )
}) })
it('should return 404 status code for default not-found page', async () => {
const res = await next.fetch('/_not-found')
expect(res.status).toBe(404)
})
it('should error on server notFound from root layout on server-side', async () => { it('should error on server notFound from root layout on server-side', async () => {
const browser = await next.browser('/?root-not-found=1') const browser = await next.browser('/?root-not-found=1')
......
...@@ -25,6 +25,11 @@ createNextDescribe( ...@@ -25,6 +25,11 @@ createNextDescribe(
) )
}) })
it('should return 404 status code for custom not-found page', async () => {
const res = await next.fetch('/_not-found')
expect(res.status).toBe(404)
})
if (isNextStart) { if (isNextStart) {
it('should include not found client reference manifest in the file trace', async () => { it('should include not found client reference manifest in the file trace', async () => {
const fileTrace = JSON.parse( const fileTrace = JSON.parse(
......
...@@ -2052,7 +2052,8 @@ ...@@ -2052,7 +2052,8 @@
"app dir - not-found - basic with default runtime should use the not-found page for non-matching routes", "app dir - not-found - basic with default runtime should use the not-found page for non-matching routes",
"app dir - not-found - basic with runtime = edge should escalate notFound to parent layout if no not-found boundary present in current layer", "app dir - not-found - basic with runtime = edge should escalate notFound to parent layout if no not-found boundary present in current layer",
"app dir - not-found - basic with runtime = edge should match dynamic route not-found boundary correctly", "app dir - not-found - basic with runtime = edge should match dynamic route not-found boundary correctly",
"app dir - not-found - basic with runtime = edge should use the not-found page for non-matching routes" "app dir - not-found - basic with runtime = edge should use the not-found page for non-matching routes",
"app dir - not-found - basic should return 404 status code for custom not-found page"
], ],
"failed": [ "failed": [
"app dir - not-found - basic should include not found client reference manifest in the file trace" "app dir - not-found - basic should include not found client reference manifest in the file trace"
......
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