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.

4 Replies

Longtail tuna
1. months ago they said they will come up with some good solution for static params with usecache (as of now it has to be async and not truly static), still waiting
2. dynamicParams
Thnx for info, i think we Will wait for Next 17 for fix this but is ok
Cinnamon Teal
When I was reading through the message I thought this is the exact scenario I used dynamicParams route segment config for in the past. https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams

But looks like it doesn't work with cacheComponents enabled.

Found this discussion: https://github.com/vercel/next.js/discussions/84991
I haven't used the root-params mentioned in there, but maybe it's a solution for you?
thnx darkstar i reed this in docs : The unstable_rootParams function has been removed. We are working on an alternative API that we will ship in an upcoming minor release.
i will still waiting for a real solution for now