Next.js Discord

Discord Forum

cacheComponents + generateStaticParams I only want this params

Unanswered
andrei posted this in #help-forum
Open in Discord
I’m implementing dynamic routes in Next.js (App Router) and I’m hitting a behavior I don’t want.

My goal is that the app only accepts routes like:

/dashboard/APARTAMENTO_3/prices

where the dynamic segment (apartamentosUnicos) must be strictly one of these three Prisma enum values:

* APARTAMENTO_1
* APARTAMENTO_2
* APARTAMENTO_3

The issue: even though I define generateStaticParams() to generate only those valid params, if someone manually types a URL like:

/dashboard/asdasd/prices

the app still resolves and renders instead of returning a clean 404.

To address it, I moved the logic into the dynamic segment by creating a layout.tsx at:

/dashboard/[apartamentosUnicos]/layout.tsx

and reading params there:

import { Apartaments } from "@prisma/client";

export function generateStaticParams() {
  return [
    { apartamentosUnicos: Apartaments.APARTAMENTO_1 },
    { apartamentosUnicos: Apartaments.APARTAMENTO_2 },
    { apartamentosUnicos: Apartaments.APARTAMENTO_3 },
  ];
}

export default async function ApartamentosLayout({
  params,
  children,
}: LayoutProps<"/dashboard/apartamentos/[apartamentosUnicos]">) {
  const { apartamentosUnicos } = await params;
  console.log("ApartamentosUnicos", apartamentosUnicos);
  return <>{children}</>;
}


But I still can’t get the exact behavior I want: any value outside those 3 enum options (e.g. asdasd) should not resolve at all and should simply return a 404.

If someone has solved this with App Router (ideally keeping static params generation and caching intact (with cahce components) ), I’d appreciate the “correct” way to hard-block invalid params so only the allowed routes exist.

1 Reply

Longtail tuna
1. months ago they said they will come with some good solution for static params (as of now it has to be async and not truly static), still waiting
2. dynamicParams