Skip to content

Commit 05262ff

Browse files
committed
ReactRouter 4 and 5
1 parent 11b1a75 commit 05262ff

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

packages/react/src/reactrouter.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
1010
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1111
getActiveSpan,
12+
getCurrentScope,
1213
getRootSpan,
1314
spanToJSON,
1415
} from '@sentry/core';
@@ -226,9 +227,13 @@ export function withSentryRouting<P extends Record<string, any>, R extends React
226227
const activeRootSpan = getActiveRootSpan();
227228

228229
const WrappedRoute: React.FC<P> = (props: P) => {
229-
if (activeRootSpan && props && props.computedMatch && props.computedMatch.isExact) {
230-
activeRootSpan.updateName(props.computedMatch.path);
231-
activeRootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
230+
if (props && props.computedMatch && props.computedMatch.isExact) {
231+
getCurrentScope().setTransactionName(props.computedMatch.path);
232+
233+
if (activeRootSpan) {
234+
activeRootSpan.updateName(props.computedMatch.path);
235+
activeRootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
236+
}
232237
}
233238

234239
// @ts-expect-error Setting more specific React Component typing for `R` generic above

packages/react/test/reactrouterv4.test.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ describe('browserTracingReactRouterV4', () => {
8686
});
8787
});
8888

89+
it("updates the scope's `transactionName` on pageload", () => {
90+
const client = createMockBrowserClient();
91+
setCurrentClient(client);
92+
93+
const history = createMemoryHistory();
94+
client.addIntegration(reactRouterV4BrowserTracingIntegration({ history }));
95+
96+
client.init();
97+
98+
expect(getCurrentScope().getScopeData()?.transactionName).toEqual('/');
99+
});
100+
89101
it('starts a navigation transaction', () => {
90102
const client = createMockBrowserClient();
91103
setCurrentClient(client);
@@ -341,4 +353,45 @@ describe('browserTracingReactRouterV4', () => {
341353
},
342354
});
343355
});
356+
357+
it("updates the scope's `transactionName` on a route change", () => {
358+
const routes: RouteConfig[] = [
359+
{
360+
path: '/organizations/:orgid/v1/:teamid',
361+
},
362+
{ path: '/organizations/:orgid' },
363+
{ path: '/' },
364+
];
365+
const client = createMockBrowserClient();
366+
setCurrentClient(client);
367+
368+
const history = createMemoryHistory();
369+
client.addIntegration(reactRouterV4BrowserTracingIntegration({ history, routes, matchPath }));
370+
371+
client.init();
372+
373+
const SentryRoute = withSentryRouting(Route);
374+
375+
render(
376+
<Router history={history as any}>
377+
<Switch>
378+
<SentryRoute path="/organizations/:orgid/v1/:teamid" component={() => <div>Team</div>} />
379+
<SentryRoute path="/organizations/:orgid" component={() => <div>OrgId</div>} />
380+
<SentryRoute path="/" component={() => <div>Home</div>} />
381+
</Switch>
382+
</Router>,
383+
);
384+
385+
act(() => {
386+
history.push('/organizations/1234/v1/758');
387+
});
388+
389+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/organizations/:orgid/v1/:teamid');
390+
391+
act(() => {
392+
history.push('/organizations/1234');
393+
});
394+
395+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/organizations/:orgid');
396+
});
344397
});

packages/react/test/reactrouterv5.test.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ describe('browserTracingReactRouterV5', () => {
8686
});
8787
});
8888

89+
it("updates the scope's `transactionName` on pageload", () => {
90+
const client = createMockBrowserClient();
91+
setCurrentClient(client);
92+
93+
const history = createMemoryHistory();
94+
client.addIntegration(reactRouterV5BrowserTracingIntegration({ history }));
95+
96+
client.init();
97+
98+
expect(getCurrentScope().getScopeData()?.transactionName).toEqual('/');
99+
});
100+
89101
it('starts a navigation transaction', () => {
90102
const client = createMockBrowserClient();
91103
setCurrentClient(client);
@@ -341,4 +353,45 @@ describe('browserTracingReactRouterV5', () => {
341353
},
342354
});
343355
});
356+
357+
it("updates the scope's `transactionName` on a route change", () => {
358+
const routes: RouteConfig[] = [
359+
{
360+
path: '/organizations/:orgid/v1/:teamid',
361+
},
362+
{ path: '/organizations/:orgid' },
363+
{ path: '/' },
364+
];
365+
const client = createMockBrowserClient();
366+
setCurrentClient(client);
367+
368+
const history = createMemoryHistory();
369+
client.addIntegration(reactRouterV5BrowserTracingIntegration({ history, routes, matchPath }));
370+
371+
client.init();
372+
373+
const SentryRoute = withSentryRouting(Route);
374+
375+
render(
376+
<Router history={history as any}>
377+
<Switch>
378+
<SentryRoute path="/organizations/:orgid/v1/:teamid" component={() => <div>Team</div>} />
379+
<SentryRoute path="/organizations/:orgid" component={() => <div>OrgId</div>} />
380+
<SentryRoute path="/" component={() => <div>Home</div>} />
381+
</Switch>
382+
</Router>,
383+
);
384+
385+
act(() => {
386+
history.push('/organizations/1234/v1/758');
387+
});
388+
389+
expect(getCurrentScope().getScopeData().transactionName).toBe('/organizations/:orgid/v1/:teamid');
390+
391+
act(() => {
392+
history.push('/organizations/1234');
393+
});
394+
395+
expect(getCurrentScope().getScopeData().transactionName).toBe('/organizations/:orgid');
396+
});
344397
});

0 commit comments

Comments
 (0)