fix(build): escape dynamic route segments in Rollup chunk names - #4393
fix(build): escape dynamic route segments in Rollup chunk names#4393enlorik wants to merge 1 commit into
Conversation
|
@enlorik is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthrough
Bracket escaping in chunk naming
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
src/build/chunks.tstest/unit/chunks.test.ts
| .find((h) => h.handler === mainId); | ||
| if (routeHandler?.route) { | ||
| return `_routes/${routeToFsPath(routeHandler.route)}.mjs`; | ||
| return `_routes/${routeToFsPath(routeHandler.route).replace(/\[([^\]]*)\]/g, "_$1_")}.mjs`; |
There was a problem hiding this comment.
🗄️ 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.
| 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.
|
Closing to address a chunk-name collision edge case flagged in review: dynamic segments escaped to |
🔗 Linked issue
Fixes #4289
❓ Type of change
📚 Description
getChunkNamereturns_routes/<path>.mjsfor server route handlers, deriving the path fromrouteToFsPath. Routes with dynamic segments (e.g./api/applications/:id/context) produce paths containing square brackets ([id]), which Rollup rejects as invalidchunkFileNamesplaceholders with"[id]" is not a valid placeholder.Applied
.replace(/\[([^\]]*)\]/g, "_$1_")to the route-derived path insidegetChunkNamebefore building the chunk name.routeToFsPathis unchanged.📝 Checklist