Unverified Commit 9697bcd3 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix notFound status code with ISR in app (#55542)

This ensures we properly set/restore the status code with ISR paths in
app router so that when we set the 404 status code with `notFound` it is
persisted properly.

Fixes: https://github.com/vercel/next.js/issues/43831
Closes: https://github.com/vercel/next.js/issues/48342
x-ref:
https://github.com/vercel/next.js/issues/49387#issuecomment-1722575969
parent 0338dcdf
...@@ -2238,6 +2238,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -2238,6 +2238,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
html: result, html: result,
pageData: metadata.pageData, pageData: metadata.pageData,
headers, headers,
status: isAppPath ? res.statusCode : undefined,
}, },
revalidate: metadata.revalidate, revalidate: metadata.revalidate,
} }
...@@ -2493,6 +2494,10 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -2493,6 +2494,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
) )
} }
if (cachedData.status) {
res.statusCode = cachedData.status
}
return { return {
type: isDataReq ? 'rsc' : 'html', type: isDataReq ? 'rsc' : 'html',
body: isDataReq body: isDataReq
......
...@@ -700,6 +700,10 @@ createNextDescribe( ...@@ -700,6 +700,10 @@ createNextDescribe(
'variable-revalidate-edge/post-method-request/page_client-reference-manifest.js', 'variable-revalidate-edge/post-method-request/page_client-reference-manifest.js',
'partial-gen-params-no-additional-lang/[lang]/[slug]/page_client-reference-manifest.js', 'partial-gen-params-no-additional-lang/[lang]/[slug]/page_client-reference-manifest.js',
'partial-gen-params-no-additional-slug/[lang]/[slug]/page_client-reference-manifest.js', 'partial-gen-params-no-additional-slug/[lang]/[slug]/page_client-reference-manifest.js',
'articles/[slug]/page.js',
'articles/[slug]/page_client-reference-manifest.js',
'articles/works.html',
'articles/works.rsc',
].sort() ].sort()
) )
}) })
...@@ -752,6 +756,22 @@ createNextDescribe( ...@@ -752,6 +756,22 @@ createNextDescribe(
"initialRevalidateSeconds": false, "initialRevalidateSeconds": false,
"srcRoute": "/", "srcRoute": "/",
}, },
"/articles/works": Object {
"dataRoute": "/articles/works.rsc",
"experimentalBypassFor": Array [
Object {
"key": "Next-Action",
"type": "header",
},
Object {
"key": "content-type",
"type": "header",
"value": "multipart/form-data",
},
],
"initialRevalidateSeconds": 1,
"srcRoute": "/articles/[slug]",
},
"/blog/seb": Object { "/blog/seb": Object {
"dataRoute": "/blog/seb.rsc", "dataRoute": "/blog/seb.rsc",
"experimentalBypassFor": Array [ "experimentalBypassFor": Array [
...@@ -1404,6 +1424,23 @@ createNextDescribe( ...@@ -1404,6 +1424,23 @@ createNextDescribe(
`) `)
expect(curManifest.dynamicRoutes).toMatchInlineSnapshot(` expect(curManifest.dynamicRoutes).toMatchInlineSnapshot(`
Object { Object {
"/articles/[slug]": Object {
"dataRoute": "/articles/[slug].rsc",
"dataRouteRegex": "^\\\\/articles\\\\/([^\\\\/]+?)\\\\.rsc$",
"experimentalBypassFor": Array [
Object {
"key": "Next-Action",
"type": "header",
},
Object {
"key": "content-type",
"type": "header",
"value": "multipart/form-data",
},
],
"fallback": null,
"routeRegex": "^\\\\/articles\\\\/([^\\\\/]+?)(?:\\\\/)?$",
},
"/blog/[author]": Object { "/blog/[author]": Object {
"dataRoute": "/blog/[author].rsc", "dataRoute": "/blog/[author].rsc",
"dataRouteRegex": "^\\\\/blog\\\\/([^\\\\/]+?)\\\\.rsc$", "dataRouteRegex": "^\\\\/blog\\\\/([^\\\\/]+?)\\\\.rsc$",
...@@ -1590,6 +1627,15 @@ createNextDescribe( ...@@ -1590,6 +1627,15 @@ createNextDescribe(
}) })
} }
it('should correctly handle statusCode with notFound + ISR', async () => {
for (let i = 0; i < 5; i++) {
const res = await next.fetch('/articles/non-existent')
expect(res.status).toBe(404)
expect(await res.text()).toContain('This page could not be found')
await waitFor(500)
}
})
it('should cache correctly for fetchCache = default-cache', async () => { it('should cache correctly for fetchCache = default-cache', async () => {
const res = await next.fetch('/default-cache') const res = await next.fetch('/default-cache')
expect(res.status).toBe(200) expect(res.status).toBe(200)
......
import { notFound } from 'next/navigation'
export interface Props {
params: {
slug: string
}
}
const Article = ({ params }: Props) => {
const { slug } = params
if (slug !== 'works') {
return notFound()
}
return <div>Articles page with slug</div>
}
export const revalidate = 1
export const dynamicParams = true
export async function generateStaticParams() {
return [
{
slug: 'works', // Anything not this should be a 404 with no ISR
},
]
}
export default Article
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