Next.js Discord

Discord Forum

Is it possible to make dynamic routes at root level in /app folder?

Answered
Sloughi posted this in #help-forum
Open in Discord
SloughiOP
I'm creating an ecommerce

Instead of

/app
/product
[slug]

I would like to do this because of SEO. This would be possible?

/app
[slug]

And this works but when I type a route that doesn't exist, instead of 404 page it appears this error:

Application error: a server-side exception has occurred (see the server logs for more information).
Answered by Sloughi
okay I fixed it. If it's useful for someone I added this:

const ProductPage = async ({ params }: { params: { slug: string } }) => {
  const { slug } = params;
  const product = await db.product.findFirst({
    where: {
      slug,
    },
  });

  if (!product) {
    return notFound();
  }

  
View full answer

3 Replies

SloughiOP
okay I fixed it. If it's useful for someone I added this:

const ProductPage = async ({ params }: { params: { slug: string } }) => {
  const { slug } = params;
  const product = await db.product.findFirst({
    where: {
      slug,
    },
  });

  if (!product) {
    return notFound();
  }

  
Answer
To add, return notFound() isn't necessary, just calling notFound() is sufficient as it returns never.
SloughiOP
thank you very much! @WebDevSayantan