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
8 changes: 4 additions & 4 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module.exports = [
path: 'packages/browser/build/npm/esm/prod/index.js',
import: createImport('init', 'browserTracingIntegration', 'replayIntegration', 'replayCanvasIntegration'),
gzip: true,
limit: '86 KB',
limit: '87 KB',
},
{
name: '@sentry/browser (incl. Tracing, Replay, Feedback)',
Expand Down Expand Up @@ -255,7 +255,7 @@ module.exports = [
path: createCDNPath('bundle.tracing.logs.metrics.min.js'),
gzip: false,
brotli: false,
limit: '131 KB',
limit: '132 KB',
},
{
name: 'CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed',
Expand All @@ -269,7 +269,7 @@ module.exports = [
path: createCDNPath('bundle.tracing.replay.min.js'),
gzip: false,
brotli: false,
limit: '245 KB',
limit: '246 KB',
},
{
name: 'CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed',
Expand Down Expand Up @@ -308,7 +308,7 @@ module.exports = [
import: createImport('init'),
ignore: ['$app/stores'],
gzip: true,
limit: '43 KB',
limit: '44 KB',
},
// Node-Core SDK (ESM)
{
Expand Down
27 changes: 27 additions & 0 deletions packages/browser/src/tracing/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ import { defaultRequestInstrumentationOptions, instrumentOutgoingRequests } from

export const BROWSER_TRACING_INTEGRATION_ID = 'BrowserTracing';

/**
* We don't want to start a bunch of idle timers and PerformanceObservers
* for web crawlers, as they may prevent the page from being seen as "idle"
* by the crawler's rendering engine (e.g. Googlebot's headless Chromium).
*/
const BOT_USER_AGENT_RE =
/Googlebot|Google-InspectionTool|Storebot-Google|Bingbot|Slurp|DuckDuckBot|Baiduspider|YandexBot|Facebot|facebookexternalhit|LinkedInBot|Twitterbot|Applebot/i;

function _isBotUserAgent(): boolean {
const nav = WINDOW.navigator as Navigator | undefined;
if (!nav?.userAgent) {
return false;
}
return BOT_USER_AGENT_RE.test(nav.userAgent);
}

interface RouteInfo {
name: string | undefined;
source: TransactionSource | undefined;
Expand Down Expand Up @@ -384,6 +400,8 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
...options,
};

const _isBot = _isBotUserAgent();

let _collectWebVitals: undefined | (() => void);
let lastInteractionTimestamp: number | undefined;

Expand Down Expand Up @@ -484,6 +502,11 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
return {
name: BROWSER_TRACING_INTEGRATION_ID,
setup(client) {
if (_isBot) {
DEBUG_BUILD && debug.log('[Tracing] Skipping browserTracingIntegration setup for bot user agent.');
return;
}

registerSpanErrorInstrumentation();

_collectWebVitals = startTrackingWebVitals({
Expand Down Expand Up @@ -630,6 +653,10 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
},

afterAllSetup(client) {
if (_isBot) {
return;
}

let startingUrl: string | undefined = getLocationHref();

if (linkPreviousTrace !== 'off') {
Expand Down
62 changes: 62 additions & 0 deletions packages/browser/test/tracing/browserTracingIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,68 @@ describe('browserTracingIntegration', () => {
Object.defineProperty(WINDOW, 'history', { value: originalGlobalHistory });
});

describe('bot user agent detection', () => {
let originalNavigator: Navigator;

beforeEach(() => {
originalNavigator = WINDOW.navigator;
});

afterEach(() => {
Object.defineProperty(WINDOW, 'navigator', { value: originalNavigator, writable: true, configurable: true });
});

function setUserAgent(ua: string): void {
Object.defineProperty(WINDOW, 'navigator', {
value: { userAgent: ua },
writable: true,
configurable: true,
});
}

it.each([
'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/W.X.Y.Z Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/W.X.Y.Z Safari/537.36',
'Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)',
'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)',
'LinkedInBot/1.0 (compatible; Mozilla/5.0)',
'Twitterbot/1.0',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15 (Applebot/0.1)',
'Mozilla/5.0 (compatible; Google-InspectionTool/1.0)',
])('skips tracing setup for bot user agent: %s', ua => {
setUserAgent(ua);

const client = new BrowserClient(
getDefaultBrowserClientOptions({
tracesSampleRate: 1,
integrations: [browserTracingIntegration()],
}),
);
setCurrentClient(client);
client.init();

expect(getActiveSpan()).toBeUndefined();
});

it('does not skip tracing setup for normal user agents', () => {
setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
);

const client = new BrowserClient(
getDefaultBrowserClientOptions({
tracesSampleRate: 1,
integrations: [browserTracingIntegration()],
}),
);
setCurrentClient(client);
client.init();

expect(getActiveSpan()).toBeDefined();
});
});

it('works with tracing enabled', () => {
const client = new BrowserClient(
getDefaultBrowserClientOptions({
Expand Down
10 changes: 9 additions & 1 deletion packages/nextjs/test/config/conflictingDebugOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ describe('debug: true + removeDebugLogging warning', () => {
let originalLocation: unknown;
let originalAddEventListener: unknown;

beforeAll(() => {
beforeAll(async () => {
// Pre-warm V8 compilation cache for the large SDK module graphs.
// Without this, the first dynamic import after vi.resetModules() can hang
// because vitest needs to compile the entire module graph from scratch.
await import('../../src/client/index.js');
await import('../../src/server/index.js');
await import('../../src/edge/index.js');
vi.resetModules();

Comment on lines +23 to +31
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated but these tests are flaky on node 20 and this fixes it

dom = new JSDOM('<!doctype html><html><head></head><body></body></html>', { url: 'https://example.com/' });

originalDocument = (globalThis as any).document;
Expand Down
Loading