@@ -107,7 +107,7 @@ function buildPrefixHierarchy(
107107 const strippedPrefix = stripLeadingDynamicSegments ( router . prefix )
108108 const segmentCount = countSegments ( strippedPrefix )
109109
110- // Skip routers with no meaningful prefix (root level)
110+ // Handle routers with no meaningful prefix
111111 if ( segmentCount === 0 ) {
112112 rootRouters . push ( router )
113113 prefixToRouter . set ( strippedPrefix , router )
@@ -147,30 +147,53 @@ function buildPrefixHierarchy(
147147 let groupRouter = prefixToRouter . get ( groupPrefix )
148148
149149 if ( ! groupRouter ) {
150- // Check if there are other routers that would be siblings under this group
151- const wouldHaveSiblings = sorted . some ( ( other ) => {
152- if ( other === router ) return false
153- const otherPrefix = stripLeadingDynamicSegments ( other . prefix )
154- const otherSegments = countSegments ( otherPrefix )
150+ // Check if there's a root-level router with matching tag that should be the parent
151+ const matchingRootRouter = rootRouters . find ( ( r ) => {
152+ if ( r === router ) return false
153+ // Router has no prefix but has a tag matching this group
154+ const rPrefix = stripLeadingDynamicSegments ( r . prefix )
155155 return (
156- otherSegments >= 2 &&
157- getPathSegments ( otherPrefix , 1 ) === groupPrefix &&
158- otherPrefix !== strippedPrefix
156+ countSegments ( rPrefix ) === 0 &&
157+ r . tags . length > 0 &&
158+ `/ ${ r . tags [ 0 ] } ` === groupPrefix
159159 )
160160 } )
161161
162- if ( wouldHaveSiblings ) {
163- // Create a synthetic group router
164- groupRouter = {
165- name : groupPrefix . replace ( / ^ \/ / , "" ) ,
166- prefix : groupPrefix ,
167- tags : [ ] ,
168- location : router . location ,
169- routes : [ ] ,
170- children : [ ] ,
162+ if ( matchingRootRouter ) {
163+ // Use this router as the group parent
164+ groupRouter = matchingRootRouter
165+ // Remove from root and re-register under the group prefix
166+ const idx = rootRouters . indexOf ( matchingRootRouter )
167+ if ( idx !== - 1 ) rootRouters . splice ( idx , 1 )
168+ matchingRootRouter . prefix = groupPrefix
169+ prefixToRouter . set ( groupPrefix , matchingRootRouter )
170+ rootRouters . push ( matchingRootRouter )
171+ } else {
172+ // Check if there are other routers that would be siblings under this group
173+ const wouldHaveSiblings = sorted . some ( ( other ) => {
174+ if ( other === router ) return false
175+ const otherPrefix = stripLeadingDynamicSegments ( other . prefix )
176+ const otherSegments = countSegments ( otherPrefix )
177+ return (
178+ otherSegments >= 2 &&
179+ getPathSegments ( otherPrefix , 1 ) === groupPrefix &&
180+ otherPrefix !== strippedPrefix
181+ )
182+ } )
183+
184+ if ( wouldHaveSiblings ) {
185+ // Create a synthetic group router
186+ groupRouter = {
187+ name : groupPrefix . replace ( / ^ \/ / , "" ) ,
188+ prefix : groupPrefix ,
189+ tags : [ ] ,
190+ location : router . location ,
191+ routes : [ ] ,
192+ children : [ ] ,
193+ }
194+ prefixToRouter . set ( groupPrefix , groupRouter )
195+ rootRouters . push ( groupRouter )
171196 }
172- prefixToRouter . set ( groupPrefix , groupRouter )
173- rootRouters . push ( groupRouter )
174197 }
175198 }
176199
0 commit comments