fix: handle undefined children in isSameRoute for sibling layouts#14788
fix: handle undefined children in isSameRoute for sibling layouts#14788scott-memco wants to merge 2 commits intoremix-run:mainfrom
Conversation
When navigating between routes in sibling layouts via RSC, the server returns route patches without a children property. In isSameRoute(), the "both empty" check on lines 5374-5379 only returns true when both routes lack children. When newRoute.children is undefined but existingRoute.children has items, execution falls through to the .every() call which crashes with "can't access property 'every', newRoute.children is undefined". Add an explicit guard after the joint empty-children check: if either route has children but the other doesn't, they are definitively different routes. This also removes the non-null assertion on newRoute.children since the guard ensures both sides have children. Fixes: remix-run#14777 Co-authored-by: Cursor <cursoragent@cursor.com>
|
|
Hi @scott-memco, Welcome, and thank you for contributing to React Router! Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once. You may review the CLA and sign it by adding your name to contributors.yml. Once the CLA is signed, the If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at hello@remix.run. Thanks! - The Remix team |
Co-authored-by: Cursor <cursoragent@cursor.com>
|
Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳 |
Summary
Fixes RSC navigation crashing with "can't access property 'every', newRoute.children is undefined" when navigating between routes in sibling layouts.
Root Cause
In
isSameRoute()(packages/react-router/lib/router/router.ts), when comparing pathless layout routes duringpatchRoutesOnNavigation:childrenpropertytruewhen both routes lack childrennewRoute.childrenisundefinedbutexistingRoute.childrenhas items, the condition evaluates tofalseand falls through tonewRoute.children!.every(...)which crashesFix
Add an explicit guard after the joint empty-children check: if either route has children but the other doesn't, they are definitively different routes. This prevents the null dereference and is semantically correct — two pathless layouts with mismatched children structures are distinct routes.
This also removes the non-null assertion (
!) onnewRoute.childrensince the guard guarantees both sides have children at that point.Reproduction
From the issue's minimal repro:
Fixes #14777
Made with Cursor