Skip to content

React router integration should use route name instead of location #10196

@ruudandriessen

Description

@ruudandriessen

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/react

SDK Version

7.93.0

Framework Version

18.2.0

Link to Sentry event

No response

SDK Setup

Sentry.init({
        dsn: '<dsn here>',
        integrations: [
            new RewriteFrames({
                iteratee: (frame) => ({
                    ...frame,
                    filename: frame.filename?.replace(document.baseURI, ''),
                }),
            }),
            new Sentry.BrowserTracing({
                routingInstrumentation: Sentry.reactRouterV6Instrumentation(
                    useEffect,
                    useLocation,
                    useNavigationType,
                    createRoutesFromChildren,
                    matchRoutes,
                ),
            }),
            new Sentry.Replay(generateSentryReplayConfig()),
        ],
})

Steps to Reproduce

Initialize sentry with react router v6.4 data API according to docs. E.g. above sentry init and:

const sentryCreateBrowserRouter =
  Sentry.wrapCreateBrowserRouter(createBrowserRouter);


export function createRouter() {
    return sentryCreateBrowserRouter(
        routes,
        { basename },
    );
}

When running this in our environment, we see transactions with the current location name show up. E.g.:
baseurl/product/1234-hash-of-this-product-id

Expected Result

We expected transactions to by grouped by the route path as defined in react router, e.g:

product/:productId

As well as without their baseurl.

The source of this seems to be:

const location = state.location;

I believe a correct implementation would involve checking the matched routes and using the route path instead:

 router.subscribe((state: RouterState) => {
     const targetRoute = state.matches
            .findLast(match => match.route.path != null)
            ?.route.path;

    if (targetRoute) {
       handleNavigation(location, routes, state.HistoryAction, undefined, basename)
   }

Actual Result

n/a

Metadata

Metadata

Assignees

Labels

BugPackage: reactIssues related to the Sentry React SDK

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions