Skip to content

Commit 1b1c513

Browse files
committed
Reduce complexity in getNormalizedName function
1 parent 4a82a6f commit 1b1c513

File tree

1 file changed

+51
-42
lines changed
  • packages/react/src/reactrouter-compat-utils

1 file changed

+51
-42
lines changed

packages/react/src/reactrouter-compat-utils/utils.ts

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ export function locationIsInsideDescendantRoute(location: Location, routes: Rout
171171
return false;
172172
}
173173

174+
/**
175+
* Returns a fallback transaction name from location pathname.
176+
*/
177+
function getFallbackTransactionName(location: Location, basename: string): string {
178+
return _stripBasename ? stripBasenameFromPathname(location.pathname, basename) : location.pathname || '';
179+
}
180+
174181
/**
175182
* Gets a normalized route name and transaction source from the current routes and location.
176183
*/
@@ -184,53 +191,55 @@ export function getNormalizedName(
184191
return [_stripBasename ? stripBasenameFromPathname(location.pathname, basename) : location.pathname, 'url'];
185192
}
186193

194+
if (!branches) {
195+
return [getFallbackTransactionName(location, basename), 'url'];
196+
}
197+
187198
let pathBuilder = '';
188199

189-
if (branches) {
190-
for (const branch of branches) {
191-
const route = branch.route;
192-
if (route) {
193-
// Early return if index route
194-
if (route.index) {
195-
return sendIndexPath(pathBuilder, branch.pathname, basename);
196-
}
197-
const path = route.path;
198-
199-
// If path is not a wildcard and has no child routes, append the path
200-
if (path && !pathIsWildcardAndHasChildren(path, branch)) {
201-
const newPath = path[0] === '/' || pathBuilder[pathBuilder.length - 1] === '/' ? path : `/${path}`;
202-
pathBuilder = trimSlash(pathBuilder) + prefixWithSlash(newPath);
203-
204-
// If the path matches the current location, return the path
205-
if (trimSlash(location.pathname) === trimSlash(basename + branch.pathname)) {
206-
if (
207-
// If the route defined on the element is something like
208-
// <Route path="/stores/:storeId/products/:productId" element={<div>Product</div>} />
209-
// We should check against the branch.pathname for the number of / separators
210-
getNumberOfUrlSegments(pathBuilder) !== getNumberOfUrlSegments(branch.pathname) &&
211-
// We should not count wildcard operators in the url segments calculation
212-
!pathEndsWithWildcard(pathBuilder)
213-
) {
214-
return [(_stripBasename ? '' : basename) + newPath, 'route'];
215-
}
216-
217-
// if the last character of the pathbuilder is a wildcard and there are children, remove the wildcard
218-
if (pathIsWildcardAndHasChildren(pathBuilder, branch)) {
219-
pathBuilder = pathBuilder.slice(0, -1);
220-
}
221-
222-
return [(_stripBasename ? '' : basename) + pathBuilder, 'route'];
223-
}
224-
}
225-
}
200+
for (const branch of branches) {
201+
const route = branch.route;
202+
if (!route) {
203+
continue;
204+
}
205+
206+
// Early return for index routes
207+
if (route.index) {
208+
return sendIndexPath(pathBuilder, branch.pathname, basename);
226209
}
227-
}
228210

229-
const fallbackTransactionName = _stripBasename
230-
? stripBasenameFromPathname(location.pathname, basename)
231-
: location.pathname || '';
211+
const path = route.path;
212+
if (!path || pathIsWildcardAndHasChildren(path, branch)) {
213+
continue;
214+
}
215+
216+
// Build the route path
217+
const newPath = path[0] === '/' || pathBuilder[pathBuilder.length - 1] === '/' ? path : `/${path}`;
218+
pathBuilder = trimSlash(pathBuilder) + prefixWithSlash(newPath);
219+
220+
// Check if this path matches the current location
221+
if (trimSlash(location.pathname) !== trimSlash(basename + branch.pathname)) {
222+
continue;
223+
}
224+
225+
// Check if this is a parameterized route like /stores/:storeId/products/:productId
226+
if (
227+
getNumberOfUrlSegments(pathBuilder) !== getNumberOfUrlSegments(branch.pathname) &&
228+
!pathEndsWithWildcard(pathBuilder)
229+
) {
230+
return [(_stripBasename ? '' : basename) + newPath, 'route'];
231+
}
232+
233+
// Handle wildcard routes with children - strip trailing wildcard
234+
if (pathIsWildcardAndHasChildren(pathBuilder, branch)) {
235+
pathBuilder = pathBuilder.slice(0, -1);
236+
}
237+
238+
return [(_stripBasename ? '' : basename) + pathBuilder, 'route'];
239+
}
232240

233-
return [fallbackTransactionName, 'url'];
241+
// Fallback when no matching route found
242+
return [getFallbackTransactionName(location, basename), 'url'];
234243
}
235244

236245
/**

0 commit comments

Comments
 (0)