Skip to content

Commit eb281f7

Browse files
committed
fix: add basePath prefix to app router navigation callback href
1 parent debc432 commit eb281f7

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ const GLOBAL_OBJ_WITH_NEXT_ROUTER = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
7272
};
7373
};
7474

75+
const globalWithInjectedBasePath = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
76+
_sentryBasePath: string | undefined;
77+
};
78+
7579
/*
7680
* The routing instrumentation needs to handle a few cases:
7781
* - Router operations:
@@ -87,7 +91,9 @@ const GLOBAL_OBJ_WITH_NEXT_ROUTER = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
8791
/** Instruments the Next.js app router for navigation. */
8892
export function appRouterInstrumentNavigation(client: Client): void {
8993
routerTransitionHandler = (href, navigationType) => {
90-
const unparameterizedPathname = new URL(href, WINDOW.location.href).pathname;
94+
const basePath = process.env._sentryBasePath ?? globalWithInjectedBasePath._sentryBasePath;
95+
const normalizedHref = basePath ? `${basePath}${href}` : href;
96+
const unparameterizedPathname = new URL(normalizedHref, WINDOW.location.href).pathname;
9197
const parameterizedPathname = maybeParameterizeRoute(unparameterizedPathname);
9298
const pathname = parameterizedPathname ?? unparameterizedPathname;
9399

@@ -206,11 +212,14 @@ function patchRouter(client: Client, router: NextRouter, currentNavigationSpanRe
206212
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
207213
};
208214

215+
const href = argArray[0];
216+
const basePath = process.env._sentryBasePath ?? globalWithInjectedBasePath._sentryBasePath;
217+
const normalizedHref = basePath ? `${basePath}${href}` : href;
209218
if (routerFunctionName === 'push') {
210-
transactionName = transactionNameifyRouterArgument(argArray[0]);
219+
transactionName = transactionNameifyRouterArgument(normalizedHref);
211220
transactionAttributes['navigation.type'] = 'router.push';
212221
} else if (routerFunctionName === 'replace') {
213-
transactionName = transactionNameifyRouterArgument(argArray[0]);
222+
transactionName = transactionNameifyRouterArgument(normalizedHref);
214223
transactionAttributes['navigation.type'] = 'router.replace';
215224
} else if (routerFunctionName === 'back') {
216225
transactionAttributes['navigation.type'] = 'router.back';

0 commit comments

Comments
 (0)