Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: add basePath prefix to app router navigation callback href
  • Loading branch information
seoyeon9888 committed Oct 13, 2025
commit eb281f74d39b0b03ea2c31e8a0dfdf8b10d40873
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ const GLOBAL_OBJ_WITH_NEXT_ROUTER = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
};
};

const globalWithInjectedBasePath = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
_sentryBasePath: string | undefined;
};

/*
* The routing instrumentation needs to handle a few cases:
* - Router operations:
Expand All @@ -87,7 +91,9 @@ const GLOBAL_OBJ_WITH_NEXT_ROUTER = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
/** Instruments the Next.js app router for navigation. */
export function appRouterInstrumentNavigation(client: Client): void {
routerTransitionHandler = (href, navigationType) => {
const unparameterizedPathname = new URL(href, WINDOW.location.href).pathname;
const basePath = process.env._sentryBasePath ?? globalWithInjectedBasePath._sentryBasePath;
const normalizedHref = basePath ? `${basePath}${href}` : href;
const unparameterizedPathname = new URL(normalizedHref, WINDOW.location.href).pathname;
const parameterizedPathname = maybeParameterizeRoute(unparameterizedPathname);
const pathname = parameterizedPathname ?? unparameterizedPathname;

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

const href = argArray[0];
const basePath = process.env._sentryBasePath ?? globalWithInjectedBasePath._sentryBasePath;
const normalizedHref = basePath ? `${basePath}${href}` : href;
if (routerFunctionName === 'push') {
transactionName = transactionNameifyRouterArgument(argArray[0]);
transactionName = transactionNameifyRouterArgument(normalizedHref);
transactionAttributes['navigation.type'] = 'router.push';
} else if (routerFunctionName === 'replace') {
transactionName = transactionNameifyRouterArgument(argArray[0]);
transactionName = transactionNameifyRouterArgument(normalizedHref);
transactionAttributes['navigation.type'] = 'router.replace';
} else if (routerFunctionName === 'back') {
transactionAttributes['navigation.type'] = 'router.back';
Expand Down