Skip to content
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
1 change: 1 addition & 0 deletions packages/browser/src/index.bundle.tracing.logs.metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export {

export {
browserTracingIntegration,
isBotUserAgent,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
} from './tracing/browserTracingIntegration';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export {

export {
browserTracingIntegration,
isBotUserAgent,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
} from './tracing/browserTracingIntegration';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {

export {
browserTracingIntegration,
isBotUserAgent,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
} from './tracing/browserTracingIntegration';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export {

export {
browserTracingIntegration,
isBotUserAgent,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
} from './tracing/browserTracingIntegration';
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/index.bundle.tracing.replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {

export {
browserTracingIntegration,
isBotUserAgent,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
} from './tracing/browserTracingIntegration';
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/index.bundle.tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export {

export {
browserTracingIntegration,
isBotUserAgent,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
} from './tracing/browserTracingIntegration';
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export { getFeedback, sendFeedback } from '@sentry-internal/feedback';
export { defaultRequestInstrumentationOptions, instrumentOutgoingRequests } from './tracing/request';
export {
browserTracingIntegration,
isBotUserAgent,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
} from './tracing/browserTracingIntegration';
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/tracing/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const BROWSER_TRACING_INTEGRATION_ID = 'BrowserTracing';
const BOT_USER_AGENT_RE =
/Googlebot|Google-InspectionTool|Storebot-Google|Bingbot|Slurp|DuckDuckBot|Baiduspider|YandexBot|Facebot|facebookexternalhit|LinkedInBot|Twitterbot|Applebot/i;

function _isBotUserAgent(): boolean {
export function isBotUserAgent(): boolean {
const nav = WINDOW.navigator as Navigator | undefined;
if (!nav?.userAgent) {
return false;
Expand Down Expand Up @@ -405,7 +405,7 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
...options,
};

const _isBot = _isBotUserAgent();
const _isBot = isBotUserAgent();

let _collectWebVitals: undefined | (() => void);
let lastInteractionTimestamp: number | undefined;
Expand Down
6 changes: 5 additions & 1 deletion packages/nextjs/src/client/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Integration } from '@sentry/core';
import { browserTracingIntegration as originalBrowserTracingIntegration } from '@sentry/react';
import { browserTracingIntegration as originalBrowserTracingIntegration, isBotUserAgent } from '@sentry/react';
import { nextRouterInstrumentNavigation, nextRouterInstrumentPageLoad } from './routing/nextRoutingInstrumentation';

/**
Expand Down Expand Up @@ -29,6 +29,10 @@ export function browserTracingIntegration(
return {
...browserTracingIntegrationInstance,
afterAllSetup(client) {
if (isBotUserAgent()) {
return;
}

// We need to run the navigation span instrumentation before the `afterAllSetup` hook on the normal browser
// tracing integration because we need to ensure the order of execution is as follows:
// Instrumentation to start span on RSC fetch request runs -> Instrumentation to put tracing headers from active span on fetch runs
Expand Down
22 changes: 22 additions & 0 deletions packages/nextjs/test/clientSdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ Object.defineProperty(global, 'addEventListener', { value: () => undefined, writ

const originalGlobalDocument = WINDOW.document;
const originalGlobalLocation = WINDOW.location;
const originalNavigator = WINDOW.navigator;
// eslint-disable-next-line @typescript-eslint/unbound-method
const originalGlobalAddEventListener = WINDOW.addEventListener;

afterAll(() => {
// Clean up JSDom
Object.defineProperty(WINDOW, 'document', { value: originalGlobalDocument });
Object.defineProperty(WINDOW, 'location', { value: originalGlobalLocation });
Object.defineProperty(WINDOW, 'navigator', { value: originalNavigator, writable: true, configurable: true });
Object.defineProperty(WINDOW, 'addEventListener', { value: originalGlobalAddEventListener });
});

Expand All @@ -43,6 +45,7 @@ describe('Client init()', () => {
getIsolationScope().clear();
getCurrentScope().clear();
getCurrentScope().setClient(undefined);
Object.defineProperty(WINDOW, 'navigator', { value: originalNavigator, writable: true, configurable: true });
});

it('inits the React SDK', () => {
Expand Down Expand Up @@ -160,6 +163,25 @@ describe('Client init()', () => {
// @ts-expect-error Test setup for build-time flag
delete globalThis.__SENTRY_TRACING__;
});

it("doesn't run Next.js router instrumentation for bot user agents", () => {
Object.defineProperty(WINDOW, 'navigator', {
value: {
userAgent: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
},
writable: true,
configurable: true,
});

const setIntervalSpy = vi.spyOn(globalThis, 'setInterval');

init({
dsn: TEST_DSN,
tracesSampleRate: 1.0,
});

expect(setIntervalSpy).not.toHaveBeenCalled();
Comment on lines +178 to +183
});
});
});

Expand Down
Loading