Skip to content

feat(browser): Add beforeStartNavigationSpan lifecycle hook #16863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions packages/browser-utils/src/metrics/cls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ export function trackClsAsStandaloneSpan(): void {
return;
}

const unsubscribeStartNavigation = client.on('startNavigationSpan', () => {
_collectClsOnce();
unsubscribeStartNavigation?.();
const unsubscribeStartNavigation = client.on('startNavigationSpan', (_, options) => {
// we only want to collect LCP if we actually navigate. Redirects should be ignored.
if (!options?.isRedirect) {
_collectClsOnce();
unsubscribeStartNavigation?.();
}
});

const activeSpan = getActiveSpan();
Expand Down
9 changes: 6 additions & 3 deletions packages/browser-utils/src/metrics/lcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ export function trackLcpAsStandaloneSpan(): void {
return;
}

const unsubscribeStartNavigation = client.on('startNavigationSpan', () => {
_collectLcpOnce();
unsubscribeStartNavigation?.();
const unsubscribeStartNavigation = client.on('startNavigationSpan', (_, options) => {
// we only want to collect LCP if we actually navigate. Redirects should be ignored.
if (!options?.isRedirect) {
_collectLcpOnce();
unsubscribeStartNavigation?.();
}
});

const activeSpan = getActiveSpan();
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/tracing/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ export function startBrowserTracingNavigationSpan(
options?: { url?: string; isRedirect?: boolean },
): Span | undefined {
const { url, isRedirect } = options || {};

client.emit('beforeStartNavigationSpan', spanOptions, { isRedirect });
client.emit('startNavigationSpan', spanOptions, { isRedirect });

const scope = getCurrentScope();
Expand Down
21 changes: 21 additions & 0 deletions packages/browser/test/tracing/browserTracingIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,27 @@ describe('browserTracingIntegration', () => {
},
});
});

it('triggers beforeStartNavigationSpan hook listeners', () => {
const client = new BrowserClient(
getDefaultBrowserClientOptions({
tracesSampleRate: 1,
integrations: [browserTracingIntegration()],
}),
);
setCurrentClient(client);

const mockBeforeStartNavigationSpanCallback = vi.fn((options: StartSpanOptions) => options);

client.on('beforeStartNavigationSpan', mockBeforeStartNavigationSpanCallback);

startBrowserTracingNavigationSpan(client, { name: 'test span', op: 'navigation' });

expect(mockBeforeStartNavigationSpanCallback).toHaveBeenCalledWith(
{ name: 'test span', op: 'navigation' },
{ isRedirect: undefined },
);
});
});

describe('using the <meta> tag data', () => {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,15 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
) => void,
): () => void;

/**
* A hook for triggering right before a navigation span is started.
* @returns {() => void} A function that, when executed, removes the registered callback.
*/
public on(
hook: 'beforeStartNavigationSpan',
callback: (options: StartSpanOptions, navigationOptions?: { isRedirect?: boolean }) => void,
): () => void;

/**
* A hook for browser tracing integrations to trigger a span for a navigation.
* @returns {() => void} A function that, when executed, removes the registered callback.
Expand Down Expand Up @@ -782,6 +791,15 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
traceOptions?: { sentryTrace?: string | undefined; baggage?: string | undefined },
): void;

/**
* Emit a hook event for triggering right before a navigation span is started.
*/
public emit(
hook: 'beforeStartNavigationSpan',
options: StartSpanOptions,
navigationOptions?: { isRedirect?: boolean },
): void;

/**
* Emit a hook event for browser tracing integrations to trigger a span for a navigation.
*/
Expand Down
28 changes: 17 additions & 11 deletions packages/nextjs/test/performance/pagesRouterInstrumentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,25 @@ describe('pagesRouterInstrumentNavigation', () => {

Router.events.emit('routeChangeStart', targetLocation);

expect(emit).toHaveBeenCalledTimes(1);
expect(emit).toHaveBeenCalledTimes(2);
const expectedStartSpanOptions = {
name: expectedTransactionName,
attributes: {
'sentry.op': 'navigation',
'sentry.origin': 'auto.navigation.nextjs.pages_router_instrumentation',
'sentry.source': expectedTransactionSource,
},
};
expect(emit).toHaveBeenCalledWith(
'startNavigationSpan',
expect.objectContaining({
name: expectedTransactionName,
attributes: {
'sentry.op': 'navigation',
'sentry.origin': 'auto.navigation.nextjs.pages_router_instrumentation',
'sentry.source': expectedTransactionSource,
},
}),
{ isRedirect: undefined },
'beforeStartNavigationSpan',
expect.objectContaining(expectedStartSpanOptions),
{
isRedirect: undefined,
},
);
expect(emit).toHaveBeenCalledWith('startNavigationSpan', expect.objectContaining(expectedStartSpanOptions), {
isRedirect: undefined,
});
},
);
});
Loading