#"Failed to collect page data" on dynamic segment route handler

8 messages · Page 1 of 1 (latest)

hard kelp
#

I have a route handler, which is defined under app/api/[id]/route.ts. This folder does not have a layout.ts nor a page.ts, it is only a route handler. When building for production, after collection page information occurs, a build error is thrown for this path. This route only contains a single GET method, which relies on the dynamic id parameter

The error is coming from a data validation, where we throw an error if the id param is invalid. Why is Next attempting to statically build my dynamic route handler?

crude raftBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

hard kelp
#

Here's the relevant code:

export const GET = async (
  _: NextRequest,
  { params }
) {
  // error is intentionally thrown if `params.id` is undefined
  const id = z.string().parse(params.id); 

  const post = await postService.getPost(id);

  return NextResponse.json({
    data: post,
  });
};
#

"Failed to collect page data" on dynamic segment route handler

hearty patrol
#

Also I tried and it's wokring with my code

#

Try to update latest version of NextJS v14

hard kelp
#

It turned out that the problem was a missing environment variable of mine which I validate with Zod, and it just wasn't clear at all that that was what was causing the error. So, word of caution in case you validate the presence of environment variables, best to include some clear error messaging in the check.