Skip to content

Commit 1932bb8

Browse files
nicohrubecclaude
andcommitted
fix: Strip trailing slashes before route pattern matching
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 59c5638 commit 1932bb8

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

packages/tanstackstart-react/src/server/routeParametrization.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ function patternToRegex(pattern: string): RegExp {
2929
* Patterns are expected to be pre-sorted by specificity (more segments first, static before dynamic).
3030
*/
3131
export function matchUrlToRoutePattern(pathname: string, patterns: string[]): string | undefined {
32+
const normalizedPathname = pathname.length > 1 ? pathname.replace(/\/$/, '') : pathname;
3233
for (const pattern of patterns) {
33-
if (patternToRegex(pattern).test(pathname)) {
34+
if (patternToRegex(pattern).test(normalizedPathname)) {
3435
return pattern;
3536
}
3637
}

packages/tanstackstart-react/test/server/routeParametrization.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ describe('matchUrlToRoutePattern', () => {
3838
const patternsNested = ['/users/$id/profile', '/users/$id'];
3939
expect(matchUrlToRoutePattern('/users/123/profile', patternsNested)).toBe('/users/$id/profile');
4040
});
41+
42+
it('handles URLs with trailing slashes', () => {
43+
expect(matchUrlToRoutePattern('/page-a/', patterns)).toBe('/page-a');
44+
expect(matchUrlToRoutePattern('/page-b/42/', patterns)).toBe('/page-b/$id');
45+
});
4146
});

0 commit comments

Comments
 (0)