Skip to content

Commit ec310ed

Browse files
committed
add integration
1 parent 24911e5 commit ec310ed

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

packages/react-router/src/client/sdk.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@ import type { BrowserOptions } from '@sentry/browser';
22
import { init as browserInit } from '@sentry/browser';
33
import type { Client } from '@sentry/core';
44
import { applySdkMetadata, setTag } from '@sentry/core';
5+
import { BROWSER_TRACING_INTEGRATION_ID } from '../../../browser/build/npm/types/tracing/browserTracingIntegration';
6+
import { reactRouterTracingIntegration } from './tracingIntegration';
57

68
/**
79
* Initializes the client side of the React Router SDK.
810
*/
911
export function init(options: BrowserOptions): Client | undefined {
10-
const opts = {
11-
...options,
12-
};
12+
// If BrowserTracing integration was passed to options, replace it with React Router tracing integration
13+
if (options.integrations && Array.isArray(options.integrations)) {
14+
const hasBrowserTracing = options.integrations.some(
15+
integration => integration.name === BROWSER_TRACING_INTEGRATION_ID,
16+
);
17+
if (hasBrowserTracing) {
18+
options.integrations = options.integrations.filter(
19+
integration => integration.name !== BROWSER_TRACING_INTEGRATION_ID,
20+
);
21+
options.integrations.push(reactRouterTracingIntegration());
22+
}
23+
}
1324

14-
applySdkMetadata(opts, 'react-router', ['react-router', 'browser']);
25+
applySdkMetadata(options, 'react-router', ['react-router', 'browser']);
1526

16-
const client = browserInit(opts);
27+
const client = browserInit(options);
1728

1829
setTag('runtime', 'browser');
1930

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { browserTracingIntegration as originalBrowserTracingIntegration } from '@sentry/browser';
2+
import type { Integration } from '@sentry/core';
3+
import { instrumentHydratedRouter } from './hydratedRouter';
4+
5+
/**
6+
* Browser tracing integration for React Router (Framework) applications.
7+
* This integration will create navigation spans and enhance transactions names with parameterized routes.
8+
*/
9+
export function reactRouterTracingIntegration(): Integration {
10+
const browserTracingIntegrationInstance = originalBrowserTracingIntegration({
11+
// Navigation transactions are started within the hydrated router instrumentation
12+
instrumentNavigation: false,
13+
});
14+
15+
return {
16+
...browserTracingIntegrationInstance,
17+
afterAllSetup(client) {
18+
browserTracingIntegrationInstance.afterAllSetup(client);
19+
instrumentHydratedRouter();
20+
},
21+
};
22+
}

0 commit comments

Comments
 (0)