Home > other >  What are the status codes returned for SSR redirect in Next.js?
What are the status codes returned for SSR redirect in Next.js?

Time:09-01

I'm using Next redirect object which allows redirecting (inside getServerSideProps), and trying to understand what status code is returned in response.

The only hint I found in the documentation is:

In some rare cases, you might need to assign a custom status code for older HTTP clients to properly redirect.

So what status code is returned by default with this implementation? There are multiple code relevant to redirecting (301, 302, 307 and more), and I found conflicting claims in the web.

CodePudding user response:

Here's a list of the possible status codes that Next.js will return based on what happens in the process of server-side rendering:

https://github.com/vercel/next.js/blob/db9040b0b85b6e111543de516961e0249faaf0e8/packages/next/pages/_error.tsx#L5

const statusCodes = {
  400: 'Bad Request',
  404: 'This page could not be found',
  405: 'Method Not Allowed',
  500: 'Internal Server Error',
}

You can also specify your own statusCode prop as part of returning an error/redirect from Next.js.

EDIT: Adding from @juliomalves, the default status codes are 307 (permanent: false) and 308 (permanent: true) for redirect!

  • Related