getServerSideProps

Version History

VersionChanges
v10.0.0locale, locales, defaultLocale, and notFound options added.
v9.3.0getServerSideProps introduced.

When exporting a function called getServerSideProps (Server-Side Rendering) from a page, Next.js will pre-render this page on each request using the data returned by getServerSideProps. This is useful if you want to fetch data that changes often, and have the page update to show the most current data.

  1. export async function getServerSideProps(context) {
  2. return {
  3. props: {}, // will be passed to the page component as props
  4. }
  5. }

You can import modules in top-level scope for use in getServerSideProps. Imports used will not be bundled for the client-side. This means you can write server-side code directly in getServerSideProps, including fetching data from your database.

Context parameter

The context parameter is an object containing the following keys:

  • params: If this page uses a dynamic route, params contains the route parameters. If the page name is [id].js , then params will look like { id: ... }.
  • req: The HTTP IncomingMessage object, with an additional cookies prop, which is an object with string keys mapping to string values of cookies.
  • res: The HTTP response object.
  • query: An object representing the query string, including dynamic route parameters.
  • preview: preview is true if the page is in the Preview Mode and false otherwise.
  • previewData: The preview data set by setPreviewData.
  • resolvedUrl: A normalized version of the request URL that strips the _next/data prefix for client transitions and includes original query values.
  • locale contains the active locale (if enabled).
  • locales contains all supported locales (if enabled).
  • defaultLocale contains the configured default locale (if enabled).

getServerSideProps return values

The getServerSideProps function should return an object with any one of the following properties:

props

The props object is a key-value pair, where each value is received by the page component. It should be a serializable object so that any props passed, could be serialized with JSON.stringify.

  1. export async function getServerSideProps(context) {
  2. return {
  3. props: { message: `Next.js is awesome` }, // will be passed to the page component as props
  4. }
  5. }

notFound

The notFound boolean allows the page to return a 404 status and 404 Page. With notFound: true, the page will return a 404 even if there was a successfully generated page before. This is meant to support use cases like user-generated content getting removed by its author.

  1. export async function getServerSideProps(context) {
  2. const res = await fetch(`https://.../data`)
  3. const data = await res.json()
  4. if (!data) {
  5. return {
  6. notFound: true,
  7. }
  8. }
  9. return {
  10. props: { data }, // will be passed to the page component as props
  11. }
  12. }

redirect

The redirect object allows redirecting to internal and external resources. It should match the shape of { destination: string, permanent: boolean }. In some rare cases, you might need to assign a custom status code for older HTTP clients to properly redirect. In these cases, you can use the statusCode property instead of the permanent property, but not both.

  1. export async function getServerSideProps(context) {
  2. const res = await fetch(`https://.../data`)
  3. const data = await res.json()
  4. if (!data) {
  5. return {
  6. redirect: {
  7. destination: '/',
  8. permanent: false,
  9. },
  10. }
  11. }
  12. return {
  13. props: {}, // will be passed to the page component as props
  14. }
  15. }

getServerSideProps with TypeScript

For TypeScript, you can use the GetServerSideProps type from next:

  1. import { GetServerSideProps } from 'next'
  2. export const getServerSideProps: GetServerSideProps = async (context) => {
  3. // ...
  4. }

If you want to get inferred typings for your props, you can use InferGetServerSidePropsType<typeof getServerSideProps>:

  1. import { InferGetServerSidePropsType } from 'next'
  2. type Data = { ... }
  3. export const getServerSideProps = async () => {
  4. const res = await fetch('https://.../data')
  5. const data: Data = await res.json()
  6. return {
  7. props: {
  8. data,
  9. },
  10. }
  11. }
  12. function Page({ data }: InferGetServerSidePropsType<typeof getServerSideProps>) {
  13. // will resolve posts to type Data
  14. }
  15. export default Page

For more information on what to do next, we recommend the following sections:

Data FetchingLearn more about data fetching in Next.js.