Skip to content

Latest commit

 

History

History
70 lines (54 loc) · 1.49 KB

invalid-redirect-gssp.mdx

File metadata and controls

70 lines (54 loc) · 1.49 KB
title
Invalid Redirect `getStaticProps` / `getServerSideProps`

Why This Error Occurred

The redirect value returned from your getStaticProps or getServerSideProps function had invalid values.

Possible Ways to Fix It

Make sure you return the proper values for the redirect value.

import type { InferGetStaticPropsType, GetStaticProps } from 'next'

type Repo = {
  name: string
  stargazers_count: number
}

export const getStaticProps = (async (context) => {
  const res = await fetch('https://api.github.com/repos/vercel/next.js')
  const repo = await res.json()

  if (!repo) {
    return {
      redirect: {
        permanent: false, // or true
        destination: '/404',
      },
    }
  }

  return { props: { repo } }
}) satisfies GetStaticProps<{
  repo: Repo
}>

export default function Page({
  repo,
}: InferGetStaticPropsType<typeof getStaticProps>) {
  return repo.stargazers_count
}
export async function getStaticPaths() {
  const res = await fetch('https://api.github.com/repos/vercel/next.js')
  const repo = await res.json()

  if (!repo) {
    return {
      redirect: {
        permanent: false, // or true
        destination: '/404',
      },
    }
  }

  return { props: { repo } }
}

export default function Page({ repo }) {
  return repo.stargazers_count
}

Useful Links