From 9715c1f4b8753bb5e600ff2a8f2b26ac2d26cfb6 Mon Sep 17 00:00:00 2001 From: Sheraff Date: Tue, 18 Nov 2025 22:03:47 +0100 Subject: [PATCH 1/6] refactor(router-core): add error when building route tree if trying to add children routes to an index route --- packages/router-core/src/route.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/router-core/src/route.ts b/packages/router-core/src/route.ts index 4b4e4404cc3..d34334c5c24 100644 --- a/packages/router-core/src/route.ts +++ b/packages/router-core/src/route.ts @@ -1675,6 +1675,15 @@ export class BaseRoute< ) } + if (process.env.NODE_ENV !== 'production') { + if (this.parentRoute && !this.parentRoute.isRoot && this.parentRoute.fullPath.endsWith('/')) { + invariant( + false, + `Parent route with id '${this.parentRoute.id}' returned by getParentRoute on '${this.id}' is an index route and cannot have child routes.`, + ) + } + } + let path: undefined | string = isRoot ? rootRouteId : options?.path // If the path is anything other than an index path, trim it up @@ -1728,6 +1737,14 @@ export class BaseRoute< TServerMiddlewares, THandlers > = (children) => { + if (process.env.NODE_ENV !== 'production') { + if (!this.isRoot && this.fullPath.endsWith('/')) { + invariant( + false, + `Cannot add children to index route '${this.id}'. Index routes cannot have child routes.`, + ) + } + } return this._addFileChildren(children) as any } From 72feec74350cbb507028e742d7b682619549c475 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 18 Nov 2025 22:34:18 +0000 Subject: [PATCH 2/6] ci: apply automated fixes --- packages/router-core/src/route.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/router-core/src/route.ts b/packages/router-core/src/route.ts index d34334c5c24..aa5efc6cade 100644 --- a/packages/router-core/src/route.ts +++ b/packages/router-core/src/route.ts @@ -1676,7 +1676,11 @@ export class BaseRoute< } if (process.env.NODE_ENV !== 'production') { - if (this.parentRoute && !this.parentRoute.isRoot && this.parentRoute.fullPath.endsWith('/')) { + if ( + this.parentRoute && + !this.parentRoute.isRoot && + this.parentRoute.fullPath.endsWith('/') + ) { invariant( false, `Parent route with id '${this.parentRoute.id}' returned by getParentRoute on '${this.id}' is an index route and cannot have child routes.`, @@ -1737,14 +1741,14 @@ export class BaseRoute< TServerMiddlewares, THandlers > = (children) => { - if (process.env.NODE_ENV !== 'production') { + if (process.env.NODE_ENV !== 'production') { if (!this.isRoot && this.fullPath.endsWith('/')) { invariant( false, `Cannot add children to index route '${this.id}'. Index routes cannot have child routes.`, ) } - } + } return this._addFileChildren(children) as any } From d58282e2e69cf21812c558b3734d91ac62f71635 Mon Sep 17 00:00:00 2001 From: Sheraff Date: Tue, 18 Nov 2025 23:37:31 +0100 Subject: [PATCH 3/6] more invariant checks --- packages/router-core/src/route.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/router-core/src/route.ts b/packages/router-core/src/route.ts index aa5efc6cade..945287706fd 100644 --- a/packages/router-core/src/route.ts +++ b/packages/router-core/src/route.ts @@ -1676,16 +1676,18 @@ export class BaseRoute< } if (process.env.NODE_ENV !== 'production') { - if ( - this.parentRoute && - !this.parentRoute.isRoot && - this.parentRoute.fullPath.endsWith('/') - ) { + if (this.parentRoute) { invariant( - false, + 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.`, ) } + if (this.parentRoute) { + 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()?`, + ) + } } let path: undefined | string = isRoot ? rootRouteId : options?.path From a7adfcb9eae9f5aa18f700ca74b37e1aedb6222e Mon Sep 17 00:00:00 2001 From: Sheraff Date: Tue, 18 Nov 2025 23:39:04 +0100 Subject: [PATCH 4/6] better use of invariant --- packages/router-core/src/route.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/router-core/src/route.ts b/packages/router-core/src/route.ts index 945287706fd..2b9f9c7a3b8 100644 --- a/packages/router-core/src/route.ts +++ b/packages/router-core/src/route.ts @@ -1744,12 +1744,10 @@ export class BaseRoute< THandlers > = (children) => { if (process.env.NODE_ENV !== 'production') { - if (!this.isRoot && this.fullPath.endsWith('/')) { - invariant( - false, - `Cannot add children to index route '${this.id}'. Index routes cannot have child routes.`, - ) - } + invariant( + this.isRoot || !this.fullPath.endsWith('/'), + `Cannot add children to index route '${this.id}'. Index routes cannot have child routes.`, + ) } return this._addFileChildren(children) as any } From 2576b28b16ea2497436150ddedde8b34f0b4c24c Mon Sep 17 00:00:00 2001 From: Sheraff Date: Tue, 18 Nov 2025 23:44:37 +0100 Subject: [PATCH 5/6] run checks after init to access id / fullPath --- packages/router-core/src/route.ts | 38 ++++++++++++++----------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/packages/router-core/src/route.ts b/packages/router-core/src/route.ts index 2b9f9c7a3b8..381a90a357a 100644 --- a/packages/router-core/src/route.ts +++ b/packages/router-core/src/route.ts @@ -1675,21 +1675,6 @@ export class BaseRoute< ) } - 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.`, - ) - } - if (this.parentRoute) { - 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()?`, - ) - } - } - let path: undefined | string = isRoot ? rootRouteId : options?.path // If the path is anything other than an index path, trim it up @@ -1722,6 +1707,23 @@ export class BaseRoute< this._id = id as TId this._fullPath = fullPath as TFullPath this._to = fullPath as TrimPathRight + + 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()?`, + ) + } + invariant( + this.children && (this.isRoot || !this.fullPath.endsWith('/')), + `Cannot add children to index route '${this.id}'. Index routes cannot have child routes.`, + ) + } } addChildren: RouteAddChildrenFn< @@ -1743,12 +1745,6 @@ export class BaseRoute< TServerMiddlewares, THandlers > = (children) => { - if (process.env.NODE_ENV !== 'production') { - invariant( - this.isRoot || !this.fullPath.endsWith('/'), - `Cannot add children to index route '${this.id}'. Index routes cannot have child routes.`, - ) - } return this._addFileChildren(children) as any } From 60e5fea9c778dfafe9d250388e6d23bb6e66b261 Mon Sep 17 00:00:00 2001 From: Sheraff Date: Tue, 18 Nov 2025 23:45:56 +0100 Subject: [PATCH 6/6] fix invariant condition --- packages/router-core/src/route.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/router-core/src/route.ts b/packages/router-core/src/route.ts index 381a90a357a..22f650abdc0 100644 --- a/packages/router-core/src/route.ts +++ b/packages/router-core/src/route.ts @@ -1719,10 +1719,12 @@ export class BaseRoute< `Parent route with id '${this.parentRoute.id}' returned by getParentRoute has no child route with id '${this.id}'. Did you forget to call .addChildren()?`, ) } - invariant( - this.children && (this.isRoot || !this.fullPath.endsWith('/')), - `Cannot add children to index route '${this.id}'. Index routes cannot have child routes.`, - ) + if (this.children) { + invariant( + this.isRoot || !this.fullPath.endsWith('/'), + `Cannot add children to index route '${this.id}'. Index routes cannot have child routes.`, + ) + } } }