Skip to main content

NextJs Error - Invalid Redirect getStaticProps/getServerSideProps

 

Both getStaticProps and getServerSideProps usually returns props but both functions can also return "redirect" object.

Redirect object is used to redirect the user to a different url. 

Invalid Redirect getStaticProps/getServerSideProps error occurs when redirect object that was returned did not have correct structure .


To fix the issue make sure the redirect object holds proper values in correct shape  { destination: string, permanent: boolean } .

  • destination requires a url path in string example "/" .
  • permanent requries a boolean value. You can instead use statusCode can also be used but not both together .
  • statusCode requires a number values . This would represent the status code .

export async function getStaticProps() {
  const data = null;

  // since data is to null if condition will be validated & redirect will occur

  if (!data) {
    return {
      redirect: {
        destination: "/",
        permanent: false,
        // statusCode: 301
      },
    };
  }

  return {
    props: { data },
  };
}


You might also encounter other errors when using the getServerSideProps :

Comments

Topics

Show more