An unusual error has recently surfaced, causing our builds to fail,
Located within the pages directory is a post/[id].tsx file that utilizes getStaticProps
and getStaticPaths
-- props
export const getStaticProps: GetStaticProps = async ({ params }) => {
const res: Response = await fetch(`${baseUrl}/api/products/${params.id}`);
const data: Product[] = await res.json();
return {
props: {
data,
},
};
};
-- paths
export const getStaticPaths: GetStaticPaths = async () => {
const res: Response = await fetch(`${baseUrl}/api/products`);
const { data } = await res.json();
const paths = data.map((product: Product) => ({
params: { id: product.id.toString() },
}));
return { paths, fallback: "blocking" };
};
When running npm run dev
locally, everything functions as intended. However, during npm run build
, an error surfaces:
Type error: Object is possibly 'undefined'.
specifically within the getStaticProps function
> 12 | const res: Response = await fetch(`${baseUrl}/api/products/${params.id}`);
^
The perplexing aspect is that the current build deployed on vercel employs identical code to construct this page with no recent modifications. Why are builds suddenly failing now?