Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js Router - Getting 404 page not found

I am new to Next.js, and I am playing around with the router. Some of the pages work, but the dynamic pages don't. Does anyone know what I'm doing wrong?

Here is my files structure:

enter image description here

The Products main page works, but when I am looping through the products and within these products, I have a link like this:

<Link href={`/products/${props.product._id}`}>

But when I click on any product, I get a 404. Does anyone know what I am doing wrong? Thanks.

like image 653
Mat Avatar asked Sep 16 '25 12:09

Mat


1 Answers

To have a /products/${props.product._id} URL, you need to create a [id] directory in the products folder, in which you add a page.tsx (leading to app/[id]/page.tsx).

That's because while with the initial pages router, a file creates a route, since Next.js v13, and when using the app router, a route is created by a folder and a page.js is used to render that route, as the doc shows it:

enter image description here

Here is (see the last question) where the CLI asks you which one of the routers you want to use:

enter image description here

Finally, if you are following along a tutorial and want to use the pages router, the easiest way is to create a new project and answer the CLI accordingly:

npx create-next-app@latest
like image 109
yousoumar Avatar answered Sep 19 '25 03:09

yousoumar