Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/router-core/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,25 @@ export class BaseRoute<
this._id = id as TId
this._fullPath = fullPath as TFullPath
this._to = fullPath as TrimPathRight<TFullPath>

if (process.env.NODE_ENV !== 'production') {
if (this.parentRoute) {
invariant(
this.parentRoute.isRoot || !this.parentRoute.fullPath.endsWith('/'),
`Parent route with id '${this.parentRoute.id}' returned by getParentRoute on '${this.id}' is an index route and cannot have child routes.`,
)
invariant(
this.parentRoute.children && this.parentRoute.children.includes(this),
`Parent route with id '${this.parentRoute.id}' returned by getParentRoute has no child route with id '${this.id}'. Did you forget to call .addChildren()?`,
)
}
if (this.children) {
invariant(
this.isRoot || !this.fullPath.endsWith('/'),
Copy link
Contributor

Choose a reason for hiding this comment

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

is the negation incorrect here?

Copy link
Contributor Author

@Sheraff Sheraff Nov 29, 2025

Choose a reason for hiding this comment

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

Hmmm in invariant the 1st param must be true to not throw, right? If this is correct, then I think the condition is good:

  • If there are children
    • the route must either be the root
    • or not be an index (i.e. not endsWith('/'))

Just checked the docs https://www.npmjs.com/package/tiny-invariant:

invariant(truthyValue, 'This should not throw!');

invariant(falsyValue, 'This will throw!');
// Error('Invariant violation: This will throw!');

This seems correct to me then

`Cannot add children to index route '${this.id}'. Index routes cannot have child routes.`,
)
}
}
}

addChildren: RouteAddChildrenFn<
Expand Down
Loading