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 :
- `undefined` cannot be serialized as JSON. Please use `null` or omit this value.
- Invalid getServerSideProps Return Value
- You can not use getStaticProps with getServerSideProps. To use SSG, please remove getServerSideProps
- Type error: 'GetServerSideProps' refers to a value, but is being used as a type here. Did you mean 'typeof GetServerSideProps'
Comments
Post a Comment
Oof!