I have a Strapi nextjs app with 2 categories of products. It's all good on local but I have this error when I try to build:
Error serializing
.product
returned fromgetStaticProps
in "/prints/[name]". Reason:undefined
cannot be serialized as JSON. Please usenull
or omit this value all together.
What I don't understand is that it concerne only one category of product while the code is the same. I know it's not the best config but I couldn't do another way.
index.js(books)
const HomePage = ({ products }) => {
return (
<div>
<Head>
<title>Catalogue</title>
<meta
name="description"
content="Classe moyenne éditions publie des livres et multiples d'artistes, émergeants ou reconnus, en France et à l'international."
/>
<meta
name="keywords"
content="Edition d'artiste, Livres, prints, multiples, art books, librairie, concept store, Bookshop, Bookstore"
/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</Head>
<Catalogue products={products} />
</div>
);
};
export async function getStaticProps() {
const products = await getProducts();
return { props: { products } };
}
export default HomePage;
index.js(prints)
const CataloguePage = ({ products }) => {
return (
<div>
<Head>
<title>Catalogue Prints</title>
<meta
name="description"
content="Classe moyenne éditions publie des livres et multiples d'artistes, émergeants ou reconnus, en France et à l'international."
/>
<meta
name="keywords"
content="Edition d'artiste, Livres, prints, multiples, art books, librairie, concept store, Bookshop, Bookstore"
/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</Head>
<CataloguePrints products={products} />
</div>
);
};
export async function getStaticProps() {
const products = await getProductsPrints();
return { props: { products } };
}
export default CataloguePage;
[name].js
export default ProductPage;
export async function getStaticProps({ params }) {
const product = await getProductPrints(params.name);
return { props: { product } };
}
// This function gets called at build time
export async function getStaticPaths() {
// Call an external API endpoint to get products
const products = await getProductsPrints();
// Get the paths we want to pre-render based on posts
const paths = products.map(
(product) => `/prints/${product.Name}`
);
// We'll pre-render only these paths at build time.
// { fallback: false } means other routes should 404.
return { paths, fallback: false };
}
If you also have some advices to avoid repeating almost same code twice i'm interested. The reason is the 2 categories have different json designs
does
await getProducts()
return an array of objects ?props
expects an object only. So you may have to refactor your code a bit before passing it to props.