I'm having an issue "typing" this implementation of getStaticProps.
As you notice the result can either return a null data return or something.
So, because of the typings in the getStaticProps, this is also getting messed up. I tried conditional props, but not working for me, any ideas?
type ProductType = {
props:
| {
product: null;
page: string,
status: number
}
| {
product: Product;
}
}
function ProductPage(props: ProductType): Children {
const { product, status, page } = props; // errors here
if (!product) {
return (
<ErrorPage statusCode={status} page={page} />
)
}
return (
<ProductPage { ...props } />
)
}
export default ProductPage;
interface StaticParams extends ParsedUrlQuery {
id: string
}
type StaticProps = {
props: {
product: ProductType;
}
}
// I get a very long error message here "StaticProps".
// Its like I can't do either "error" values or valid values.
export const getStaticProps: GetStaticProps<StaticProps, StaticParams> = async (context) => {
const params = context.params!
const response = await fetch(`${process.env.NEXT_PUBLIC_HOST}/products/${params.id}`);
const product = await response.json();
if (!product || product.errors) {
return {
props: {
product: null,
page: 'error page',
status: 404
}
}
}
return {
props: {
product: { ...product },
},
revalidate: 10
}
}