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

Respect non 200 status to page static generation response #63731

Merged
merged 10 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
19 changes: 15 additions & 4 deletions packages/next/src/export/routes/app-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,23 @@ export async function exportAppPage(
'utf8'
)

const isParallelRoute = /\/@\w+/.test(page)
huozhi marked this conversation as resolved.
Show resolved Hide resolved
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.
const meta: RouteMetadata = {
// 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.
status: renderOpts.experimental.ppr ? res.statusCode : undefined,
status,
headers,
postponed,
}
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/app-dir/static-generation-status/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { notFound } from 'next/navigation'

export default function Page() {
notFound()
}

export const dynamic = 'force-static'
3 changes: 3 additions & 0 deletions test/e2e/app-dir/static-generation-status/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'home'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { redirect } from 'next/navigation'

export default function Page() {
redirect('/')
}

export const dynamic = 'force-static'
25 changes: 25 additions & 0 deletions test/e2e/app-dir/static-generation-status/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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)
})
}
)