Skip to content

fix(build): escape dynamic route segments in Rollup chunk names - #4393

Closed
enlorik wants to merge 1 commit into
nitrojs:mainfrom
enlorik:fix/escape-dynamic-route-chunk-names
Closed

fix(build): escape dynamic route segments in Rollup chunk names#4393
enlorik wants to merge 1 commit into
nitrojs:mainfrom
enlorik:fix/escape-dynamic-route-chunk-names

Conversation

@enlorik

@enlorik enlorik commented Jun 29, 2026

Copy link
Copy Markdown

🔗 Linked issue

Fixes #4289

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

getChunkName returns _routes/<path>.mjs for server route handlers, deriving the path from routeToFsPath. Routes with dynamic segments (e.g. /api/applications/:id/context) produce paths containing square brackets ([id]), which Rollup rejects as invalid chunkFileNames placeholders with "[id]" is not a valid placeholder.

Applied .replace(/\[([^\]]*)\]/g, "_$1_") to the route-derived path inside getChunkName before building the chunk name. routeToFsPath is unchanged.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@enlorik
enlorik requested a review from pi0 as a code owner June 29, 2026 00:27
@vercel

vercel Bot commented Jun 29, 2026

Copy link
Copy Markdown

@enlorik is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

getChunkName in src/build/chunks.ts now replaces bracketed dynamic route segments (e.g. [id]) with underscore-delimited equivalents (e.g. _id_) before using the path as a Rollup chunkFileNames pattern. Unit tests are updated to expect the escaped form and a new test covers nested dynamic routes.

Bracket escaping in chunk naming

Layer / File(s) Summary
Bracket escaping fix and tests
src/build/chunks.ts, test/unit/chunks.test.ts
getChunkName replaces [param] with _param_ in the route-derived fs path before appending .mjs. The existing dynamic-route test expectation is updated to _id_ and a new test asserts correct escaping for a nested dynamic route (_routes/api/applications/_id_/context.mjs).

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

  • nitrojs/nitro#3849: Modifies the same getChunkName logic in src/build/chunks.ts for _routes/*.mjs filename generation.
  • nitrojs/nitro#4027: Changes dynamic route segment transformation via routeToFsPath in the same files (src/build/chunks.ts, test/unit/chunks.test.ts).
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title uses the conventional commits format and matches the build fix.
Description check ✅ Passed The description clearly describes the Rollup chunk-name escaping fix.
Linked Issues check ✅ Passed The change sanitizes bracketed route segments and matches issue #4289's expected fix.
Out of Scope Changes check ✅ Passed The only code and test changes are directly related to chunk-name escaping.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/build/chunks.ts`:
- Line 81: The chunk name generation in chunks.ts uses routeToFsPath() plus a
bracket-to-underscore replacement, which can collide between dynamic routes and
literal paths (for example, /api/:id and /api/_id_). Update the chunk-name
mapping to escape dynamic segments into a form routeToFsPath() can never emit
for literal routes, and keep the change localized to the route handler chunk
naming logic. Add a regression test that exercises the collision case and
verifies the two routes produce distinct chunk names.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7511372c-9402-4dea-8554-15354bceac73

📥 Commits

Reviewing files that changed from the base of the PR and between 3df6960 and 9c94928.

📒 Files selected for processing (2)
  • src/build/chunks.ts
  • test/unit/chunks.test.ts

Comment thread src/build/chunks.ts
.find((h) => h.handler === mainId);
if (routeHandler?.route) {
return `_routes/${routeToFsPath(routeHandler.route)}.mjs`;
return `_routes/${routeToFsPath(routeHandler.route).replace(/\[([^\]]*)\]/g, "_$1_")}.mjs`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Avoid route/literal chunk-name collisions.

[id] -> _id_ shares the same namespace as literal segments, so /api/:id and /api/_id_ now both emit _routes/api/_id_.mjs. That can make the build reuse or overwrite the wrong artifact. Please escape dynamic segments to a form that routeToFsPath() can never produce for literal paths (for example, encoding the brackets themselves) and add a regression test for that collision.

Suggested fix
-      return `_routes/${routeToFsPath(routeHandler.route).replace(/\[([^\]]*)\]/g, "_$1_")}.mjs`;
+      return `_routes/${routeToFsPath(routeHandler.route).replace(/\[/g, "%5B").replace(/\]/g, "%5D")}.mjs`;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return `_routes/${routeToFsPath(routeHandler.route).replace(/\[([^\]]*)\]/g, "_$1_")}.mjs`;
return `_routes/${routeToFsPath(routeHandler.route).replace(/\[/g, "%5B").replace(/\]/g, "%5D")}.mjs`;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/build/chunks.ts` at line 81, The chunk name generation in chunks.ts uses
routeToFsPath() plus a bracket-to-underscore replacement, which can collide
between dynamic routes and literal paths (for example, /api/:id and /api/_id_).
Update the chunk-name mapping to escape dynamic segments into a form
routeToFsPath() can never emit for literal routes, and keep the change localized
to the route handler chunk naming logic. Add a regression test that exercises
the collision case and verifies the two routes produce distinct chunk names.

@enlorik

enlorik commented Jun 29, 2026

Copy link
Copy Markdown
Author

Closing to address a chunk-name collision edge case flagged in review: dynamic segments escaped to _id_ share the namespace with literal route segments of the same name. Will reopen with a fix that encodes the brackets themselves.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested dynamic API route causes invalid Rollup chunkFileNames placeholder

1 participant