Unverified Commit 93aac0e3 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Respect non 200 status to page static generation response (#63731)

### What

In static generation phase of app page, if there's any case that we're
receiving 3xx/4xx status code from the response, we 're setting it into
the static generation meta now to make sure they're still returning the
same status after build.

### Why

During static generation if there's any 3xx/4xx status code that is set
in the response, we should respect to it, such as the ones caused by
using `notFound()` to mark as 404 response or `redirect` to mark as
`307` response.

Closes NEXT-2895
Fixes #51021
Fixes #62228
parent 98e6fc58
...@@ -128,12 +128,23 @@ export async function exportAppPage( ...@@ -128,12 +128,23 @@ export async function exportAppPage(
'utf8' 'utf8'
) )
const isParallelRoute = /\/@\w+/.test(page)
const isNonSuccessfulStatusCode = res.statusCode > 300
// When PPR is enabled, we don't always send 200 for routes that have been
// pregenerated, so we should grab the status code from the mocked
// response.
let status: number | undefined = renderOpts.experimental.ppr
? res.statusCode
: undefined
// If it's parallel route the status from mock response is 404
if (isNonSuccessfulStatusCode && !isParallelRoute) {
status = res.statusCode
}
// Writing the request metadata to a file. // Writing the request metadata to a file.
const meta: RouteMetadata = { const meta: RouteMetadata = {
// When PPR is enabled, we don't always send 200 for routes that have been status,
// pregenerated, so we should grab the status code from the mocked
// response.
status: renderOpts.experimental.ppr ? res.statusCode : undefined,
headers, headers,
postponed, postponed,
} }
......
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { notFound } from 'next/navigation'
export default function Page() {
notFound()
}
export const dynamic = 'force-static'
export default function Page() {
return 'home'
}
import { redirect } from 'next/navigation'
export default function Page() {
redirect('/')
}
export const dynamic = 'force-static'
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app-dir static-generation-status',
{
files: __dirname,
},
({ next }) => {
it('should render the page using notFound with status 404', async () => {
const { status } = await next.fetch('/not-found-page')
expect(status).toBe(404)
})
it('should render the page using redirect with status 307', async () => {
const { status } = await next.fetch('/redirect-page', {
redirect: 'manual',
})
expect(status).toBe(307)
})
it('should render the non existed route redirect with status 404', async () => {
expect((await next.fetch('/does-not-exist')).status).toBe(404)
})
}
)
export default function RootLayout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Default() {
return '@foo default'
}
export default function Layout({ children, bar, foo }) {
return (
<div>
<h1>Layout</h1>
<div id="foo-slot">{foo}</div>
<div id="bar-slot">{bar}</div>
</div>
)
}
export default function Page() {
return <div>Hello from Nested</div>
}
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app-dir parallel-routes-static',
{
files: __dirname,
},
({ next }) => {
it('should static generate parallel routes', async () => {
const rscExtension = process.env.__NEXT_EXPERIMENTAL_PPR
? '.prefetch.rsc'
: '.rsc'
expect(await next.hasFile('.next/server/app/nested/foo.html')).toBe(true)
expect(await next.hasFile('.next/server/app/nested/foo.meta')).toBe(true)
expect(
await next.hasFile(`.next/server/app/nested/foo${rscExtension}`)
).toBe(true)
expect(await next.hasFile('.next/server/app/nested/bar.html')).toBe(true)
expect(await next.hasFile('.next/server/app/nested/bar.meta')).toBe(true)
expect(
await next.hasFile(`.next/server/app/nested/bar${rscExtension}`)
).toBe(true)
})
}
)
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