Skip to content

Conversation

@bukamasedo
Copy link

What?

Improve error messages when non-string values are passed to getStaticPaths or generateStaticParams.

Why?

When developers accidentally pass non-string values (e.g., numbers) to path parameters, the current error message is cryptic:

TypeError: segment.replace is not a function

This makes debugging difficult, especially for beginners.

How?

  1. Added type validation in escapePathDelimiters() function to check if the segment is a string
  2. Added array element validation in pages.ts for catch-all routes ([...slug])
  3. Added the same validation in app.ts for generateStaticParams

Now the error message is clear:

A path segment must be a string, received number (123).
This typically happens when a non-string value is passed to getStaticPaths.
Make sure all path parameters are strings.

Testing

  • Added unit tests for escapePathDelimiters with invalid inputs
  • Ran existing unit tests (pnpm test-unit)
  • Ran lint and prettier checks

fixes #41281

When developers accidentally pass non-string values to path parameters,
the error message is now clear instead of cryptic TypeError.

fixes vercel#41281
@nextjs-bot
Copy link
Collaborator

nextjs-bot commented Feb 3, 2026

Allow CI Workflow Run

  • approve CI run for commit: 3a10f2c

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

}

// Ensure all array elements are strings
const invalidElement = paramValue.find(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only catch one value instead of all values that are incorrect. Given that .find will loop through all parameters if there is no invalid element it's likely better to collect all of them

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timneutkens
Good point! Updated to use filter() to collect all invalid elements instead of just the first one.
The error message now shows all invalid values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent Error Messaging / Handling in getStaticPaths

3 participants