Skip to content

Incorrect display of custom error page when setting status code #58654

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

Open
1 task done
WhidRubeld opened this issue Nov 19, 2023 · 2 comments
Open
1 task done

Incorrect display of custom error page when setting status code #58654

WhidRubeld opened this issue Nov 19, 2023 · 2 comments
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js. stale The issue has not seen recent activity.

Comments

@WhidRubeld
Copy link

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/gracious-bassi-lpmwvf

To Reproduce

  1. Set up page
// pages/index.tsx

import type { GetServerSideProps } from 'next'

const Page = () => {
  return <p>Hello</p>
}

export const getServerSideProps: GetServerSideProps = async ({ res }) => {
  res.statusCode = 403
 
  // res.end() // If specified, the render will be empty
  // throw new Error()

  return { props: {} }
}

export default Page
  1. Set up custom error page
// pages/_error.tsx

import { NextPageContext } from 'next/types'

const Page = ({ statusCode }: { statusCode: number }) => {
  return (
    <p>
      {statusCode
        ? `An error ${statusCode} occurred on server`
        : 'An error occurred on client'}
    </p>
  )
}

Page.getInitialProps = ({ res, err }: NextPageContext) => {
  const statusCode = res ? res.statusCode : err ? err.statusCode : 404
  return { statusCode }
}

export default Page
  1. yarn build & yarn start

Current vs. Expected behavior

Setting the status should have resulted in _error.tsx being rendered. Instead, the HTTP status is set along with the rendering index.tsx.

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:28:12 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8103
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 14.0.3
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware / Edge (API routes, runtime)

Additional context

No response

@WhidRubeld WhidRubeld added the bug Issue was opened via the bug report template. label Nov 19, 2023
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Nov 19, 2023
@WhidRubeld
Copy link
Author

Similar problem #56075

@nextjs-bot
Copy link
Collaborator

This issue has been automatically marked as stale due to two years of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.

@nextjs-bot nextjs-bot added the stale The issue has not seen recent activity. label May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js. stale The issue has not seen recent activity.
Projects
None yet
Development

No branches or pull requests

2 participants