Skip to content

Commit 8e894db

Browse files
committed
fix: paramaterization for root route
1 parent c1f69ce commit 8e894db

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/nextjs/src/client/routing/parameterization.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ function findMatchingRoutes(
152152
if (dynamicRoute.hasOptionalPrefix && dynamicRoute.regex) {
153153
// Prepend a placeholder segment to simulate the optional prefix
154154
// e.g., '/foo' becomes '/PLACEHOLDER/foo' to match '/:locale/foo'
155-
const routeWithPrefix = `/SENTRY_OPTIONAL_PREFIX${route}`;
155+
// Special case: '/' becomes '/PLACEHOLDER' (not '/PLACEHOLDER/') to match '/:locale' pattern
156+
const routeWithPrefix = route === '/' ? '/SENTRY_OPTIONAL_PREFIX' : `/SENTRY_OPTIONAL_PREFIX${route}`;
156157
const regex = getCompiledRegex(dynamicRoute.regex);
157158
if (regex?.test(routeWithPrefix)) {
158159
matches.push(dynamicRoute.path);

packages/nextjs/test/client/parameterization.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,5 +900,37 @@ describe('maybeParameterizeRoute', () => {
900900
'/:locale/users/:userId/posts/:postId/comments/:commentId',
901901
);
902902
});
903+
904+
it('should handle root path with optional locale prefix', () => {
905+
const manifest: RouteManifest = {
906+
staticRoutes: [],
907+
dynamicRoutes: [
908+
{
909+
path: '/:locale',
910+
regex: '^/([^/]+)$',
911+
paramNames: ['locale'],
912+
hasOptionalPrefix: true,
913+
},
914+
{
915+
path: '/:locale/about',
916+
regex: '^/([^/]+)/about$',
917+
paramNames: ['locale'],
918+
hasOptionalPrefix: true,
919+
},
920+
],
921+
};
922+
globalWithInjectedManifest._sentryRouteManifest = JSON.stringify(manifest);
923+
924+
// Root path without locale prefix (default locale)
925+
expect(maybeParameterizeRoute('/')).toBe('/:locale');
926+
927+
// Root path with locale prefix
928+
expect(maybeParameterizeRoute('/en')).toBe('/:locale');
929+
expect(maybeParameterizeRoute('/ar')).toBe('/:locale');
930+
931+
// Nested routes still work
932+
expect(maybeParameterizeRoute('/about')).toBe('/:locale/about');
933+
expect(maybeParameterizeRoute('/fr/about')).toBe('/:locale/about');
934+
});
903935
});
904936
});

0 commit comments

Comments
 (0)