Skip to content

Commit

Permalink
feat: set status code to 500 if unexpected error occurs before stream…
Browse files Browse the repository at this point in the history
…ing in app router (#56236)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

This PR therefore introduces to always set response status code to 500
unless it is a `NotFoundError` or `RedirectError`. This PR would fix
issue #56235. See also:
https://codesandbox.io/p/sandbox/nice-panini-2z3mcp .

**Current Behavior**
At the moment, when an unexpected error occurs during app server
rendering, a 200 ok is returned as status code. This seems to be
undesirable because of the success status CDNs will cache the error
pages and crawlers will index the page considering the error content as
the actual content.

**Desired Behavior**
This issue is related to discussion
#53225. Even though I
understand that the response status code cannot be set if streaming has
started, in my view it would be best to set the response status to 500
whenever it can (so before the streaming has started) for SEO and (CDN)
http caching. This would also be consistent with how 404s currently
work; that is, response status code is set to 404 if `NotFoundError`
occurred before streaming (related
[issue](#43831) &
[PR](#55542)).

Ideally, when a runtime error happens after streaming, a `<meta
name="robots" content="noindex" />` would also be added. But I didn't
want to make the PR too complex before receiving feedback.

---------

Co-authored-by: Vũ Văn Dũng <[email protected]>
Co-authored-by: Tobias Koppers <[email protected]>
  • Loading branch information
3 people authored Oct 17, 2023
1 parent ee9bee9 commit 218d070
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,9 @@ async function renderToHTMLOrFlightImpl(
}

const is404 = res.statusCode === 404
if (!is404 && !hasRedirectError) {
res.statusCode = 500
}

// Preserve the existing RSC inline chunks from the page rendering.
// To avoid the same stream being operated twice, clone the origin stream for error rendering.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { use } from 'react'

function getData({ params }) {
async function getData({ params }) {
return {
now: Date.now(),
params,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const revalidate = 1

export default function UnexpectedErrorPage(props) {
// use query param to only throw error during runtime, not build time
if (props.searchParams.error) {
throw new Error('Oh no')
}
return <p id="page">/unexpected-error</p>
}
7 changes: 7 additions & 0 deletions test/production/app-dir/unexpected-error/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Loading() {
return <p>loading</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function UnexpectedErrorPage(props) {
// use query param to only throw error during runtime, not build time
if (props.searchParams.error) {
throw new Error('Oh no')
}
return <p id="page">/unexpected-error</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function UnexpectedErrorPage(props) {
// use query param to only throw error during runtime, not build time
if (props.searchParams.error) {
throw new Error('Oh no')
}
return <p id="page">/unexpected-error</p>
}
6 changes: 6 additions & 0 deletions test/production/app-dir/unexpected-error/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig
26 changes: 26 additions & 0 deletions test/production/app-dir/unexpected-error/unexpected-error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createNextDescribe } from 'e2e-utils'

createNextDescribe(
'unexpected-error',
{
files: __dirname,
},
({ next }) => {
it('should set response status to 500 for unexpected errors in ssr app route', async () => {
const res = await next.fetch('/ssr-unexpected-error?error=true')
expect(res.status).toBe(500)
})

it('cannot change response status when streaming has started', async () => {
const res = await next.fetch(
'/ssr-unexpected-error-after-streaming?error=true'
)
expect(res.status).toBe(200)
})

it('should set response status to 500 for unexpected errors in isr app route', async () => {
const res = await next.fetch('/isr-unexpected-error?error=true')
expect(res.status).toBe(500)
})
}
)

1 comment on commit 218d070

@ijjk
Copy link
Member

@ijjk ijjk commented on 218d070 Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stats from current release

Default Build (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary v13.5.5 vercel/next.js canary Change
buildDuration 10.1s 11s ⚠️ +989ms
buildDurationCached 6.3s 6.2s N/A
nodeModulesSize 172 MB 174 MB ⚠️ +1.32 MB
nextStartRea..uration (ms) 537ms 531ms N/A
Client Bundles (main, webpack)
vercel/next.js canary v13.5.5 vercel/next.js canary Change
199-HASH.js gzip 27.5 kB 27.5 kB N/A
3f784ff6-HASH.js gzip 50.9 kB N/A N/A
99.HASH.js gzip 182 B 182 B
framework-HASH.js gzip 45.3 kB 45.3 kB
main-app-HASH.js gzip 254 B 252 B N/A
main-HASH.js gzip 32.9 kB 32.9 kB N/A
webpack-HASH.js gzip 1.75 kB 1.75 kB N/A
3c4a14c2-HASH.js gzip N/A 53.1 kB N/A
Overall change 45.5 kB 45.5 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary v13.5.5 vercel/next.js canary Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary v13.5.5 vercel/next.js canary Change
_app-HASH.js gzip 206 B 205 B N/A
_error-HASH.js gzip 182 B 180 B N/A
amp-HASH.js gzip 506 B 505 B N/A
css-HASH.js gzip 322 B 323 B N/A
dynamic-HASH.js gzip 2.57 kB 2.57 kB N/A
edge-ssr-HASH.js gzip 260 B 259 B N/A
head-HASH.js gzip 350 B 350 B
hooks-HASH.js gzip 369 B 369 B
image-HASH.js gzip 4.35 kB 4.35 kB N/A
index-HASH.js gzip 256 B 256 B
link-HASH.js gzip 2.64 kB 2.63 kB N/A
routerDirect..HASH.js gzip 312 B 311 B N/A
script-HASH.js gzip 385 B 384 B N/A
withRouter-HASH.js gzip 307 B 308 B N/A
1afbb74e6ecf..834.css gzip 106 B 106 B
Overall change 1.08 kB 1.08 kB
Client Build Manifests
vercel/next.js canary v13.5.5 vercel/next.js canary Change
_buildManifest.js gzip 485 B 482 B N/A
Overall change 0 B 0 B
Rendered Page Sizes
vercel/next.js canary v13.5.5 vercel/next.js canary Change
index.html gzip 529 B 529 B
link.html gzip 542 B 543 B N/A
withRouter.html gzip 525 B 524 B N/A
Overall change 529 B 529 B
Edge SSR bundle Size Overall increase ⚠️
vercel/next.js canary v13.5.5 vercel/next.js canary Change
edge-ssr.js gzip 93.7 kB 93.7 kB N/A
page.js gzip 152 kB 154 kB ⚠️ +2.42 kB
Overall change 152 kB 154 kB ⚠️ +2.42 kB
Middleware size
vercel/next.js canary v13.5.5 vercel/next.js canary Change
middleware-b..fest.js gzip 625 B 621 B N/A
middleware-r..fest.js gzip 150 B 151 B N/A
middleware.js gzip 22.5 kB 22.5 kB N/A
edge-runtime..pack.js gzip 1.92 kB 1.92 kB
Overall change 1.92 kB 1.92 kB
Diff details
Diff for page.js

Diff too large to display

Diff for 199-HASH.js

Diff too large to display

Diff for 3f784ff6-HASH.js

Diff too large to display

Diff for main-HASH.js

Diff too large to display

Please sign in to comment.