First time using NextJS to build an application. I'm trying to fetch products from the database.
My folder structure for the products is as follows:
products
|__[category]
| |__page.tsx
| |__[subcategory]
| |__page.tsx
|_page.tsx
Now the problem is not so much with the data fetching, but that the getServerSideProps-function is not even being triggered. This is what my [category] -> page.tsx
looks like:
import ProductsPageLayout from '../components/ProductsPageLayout';
export default function CategoryProducts({ products }: any) {
return (
<>
{JSON.stringify('PRODUCTS - C ', products)}
<ProductsPageLayout products={products} />
</>
);
}
export async function getServerSideProps(context: any) {
console.log('LOGGING!!!!!!!')
const { category } = context.params;
const res = await fetch(`http://xxx:xxxx/api/products/${category}`);
const products = await res.json();
console.log('C - CONTEXT ', context);
console.log('C - RES ', res);
console.log('C - REPO ', products);
return { props: { products } };
}
So what would the problem be? I have tried everything I can and whatever I do the second function (getServerSideProps) is not being triggered. Am I missing any setup, is my folder structure wrong? I haven't done anything to my next.config.js
file. I know it is a rookie problem, but I can't figure it out whatever I do.