Skip to content
Draft
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
42 changes: 30 additions & 12 deletions packages/react/src/tanstackrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {
WINDOW,
} from '@sentry/browser';
import type { Integration } from '@sentry/core/browser';
import { filterCollectedUrl, hasSpanStreamingEnabled, PAGELOAD_SPAN_NAME_FALLBACK } from '@sentry/core';
import {
createUrlRouteProvider,
filterCollectedUrl,
hasSpanStreamingEnabled,
PAGELOAD_SPAN_NAME_FALLBACK,
setRouteProvider,
} from '@sentry/core';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
Expand Down Expand Up @@ -50,22 +56,34 @@ export function tanstackRouterBrowserTracingIntegration(

const { instrumentPageLoad = true, instrumentNavigation = true } = options;

const resolveRouteMatch = (pathname: string, search: unknown): VendoredTanstackRouterRouteMatch | undefined => {
const matchedRoutes = castRouterInstance.matchRoutes(pathname, search as {}, {
preload: false,
throwOnError: false,
});
const lastMatch = matchedRoutes[matchedRoutes.length - 1];
// If we only match __root__, we ended up not matching any route at all, so
// we fall back to the pathname.
return lastMatch?.routeId !== '__root__' ? lastMatch : undefined;
};

return {
...browserTracingIntegrationInstance,
setup(client) {
// Registered before `afterAllSetup` so the provider is in place by the time the pageload span
// is named.
setRouteProvider(
createUrlRouteProvider(
url => resolveRouteMatch(url.pathname, castRouterInstance.options.parseSearch(url.search))?.routeId,
),
client,
);

browserTracingIntegrationInstance.setup?.(client);
},
afterAllSetup(client) {
browserTracingIntegrationInstance.afterAllSetup(client);

const resolveRouteMatch = (pathname: string, search: unknown): VendoredTanstackRouterRouteMatch | undefined => {
const matchedRoutes = castRouterInstance.matchRoutes(pathname, search as {}, {
preload: false,
throwOnError: false,
});
const lastMatch = matchedRoutes[matchedRoutes.length - 1];
// If we only match __root__, we ended up not matching any route at all, so
// we fall back to the pathname.
return lastMatch?.routeId !== '__root__' ? lastMatch : undefined;
};

const applyRouteMatch = (
span: NonNullable<ReturnType<typeof startBrowserTracingPageLoadSpan>>,
match: VendoredTanstackRouterRouteMatch | undefined,
Expand Down
28 changes: 20 additions & 8 deletions packages/solid/src/tanstackrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import {
} from '@sentry/conventions/attributes';
import type { Integration } from '@sentry/core';
import {
createUrlRouteProvider,
hasSpanStreamingEnabled,
PAGELOAD_SPAN_NAME_FALLBACK,
setRouteProvider,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
Expand Down Expand Up @@ -51,19 +53,29 @@ export function tanstackRouterBrowserTracingIntegration<R extends AnyRouter>(

const { instrumentPageLoad = true, instrumentNavigation = true } = options;

const resolveRouteMatch = (pathname: string, search: Record<string, unknown>): RouteMatch | undefined => {
const matchedRoutes = router.matchRoutes(pathname, search, { preload: false, throwOnError: false });
const lastMatch = matchedRoutes[matchedRoutes.length - 1];
// If we only match __root__, we ended up not matching any route at all, so
// we fall back to the pathname.
return lastMatch?.routeId !== '__root__' ? lastMatch : undefined;
};

return {
...browserTracingIntegrationInstance,
setup(client) {
// Registered before `afterAllSetup` so the provider is in place by the time the pageload span
// is named.
setRouteProvider(
createUrlRouteProvider(url => resolveRouteMatch(url.pathname, router.options.parseSearch(url.search))?.routeId),
client,
);

browserTracingIntegrationInstance.setup?.(client);
},
afterAllSetup(client) {
browserTracingIntegrationInstance.afterAllSetup(client);

const resolveRouteMatch = (pathname: string, search: Record<string, unknown>): RouteMatch | undefined => {
const matchedRoutes = router.matchRoutes(pathname, search, { preload: false, throwOnError: false });
const lastMatch = matchedRoutes[matchedRoutes.length - 1];
// If we only match __root__, we ended up not matching any route at all, so
// we fall back to the pathname.
return lastMatch?.routeId !== '__root__' ? lastMatch : undefined;
};

const applyRouteMatch = (
span: NonNullable<ReturnType<typeof startBrowserTracingPageLoadSpan>>,
match: RouteMatch | undefined,
Expand Down
30 changes: 21 additions & 9 deletions packages/vue/src/tanstackrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import {
} from '@sentry/conventions/attributes';
import type { Integration } from '@sentry/core';
import {
createUrlRouteProvider,
hasSpanStreamingEnabled,
PAGELOAD_SPAN_NAME_FALLBACK,
setRouteProvider,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
Expand Down Expand Up @@ -56,20 +58,30 @@ export function tanstackRouterBrowserTracingIntegration<R extends AnyRouter>(

const { instrumentPageLoad = true, instrumentNavigation = true } = options;

const resolveRouteMatch = (pathname: string, search: unknown): RouteMatch | undefined => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const matchedRoutes = router.matchRoutes(pathname, search as any, { preload: false, throwOnError: false });
const lastMatch = matchedRoutes[matchedRoutes.length - 1];
// If we only match __root__, we ended up not matching any route at all, so
// we fall back to the pathname.
return lastMatch?.routeId !== '__root__' ? lastMatch : undefined;
};

return {
...browserTracingIntegrationInstance,
setup(client) {
// Registered before `afterAllSetup` so the provider is in place by the time the pageload span
// is named.
setRouteProvider(
createUrlRouteProvider(url => resolveRouteMatch(url.pathname, router.options.parseSearch(url.search))?.routeId),
client,
);

browserTracingIntegrationInstance.setup?.(client);
},
afterAllSetup(client) {
browserTracingIntegrationInstance.afterAllSetup(client);

const resolveRouteMatch = (pathname: string, search: unknown): RouteMatch | undefined => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const matchedRoutes = router.matchRoutes(pathname, search as any, { preload: false, throwOnError: false });
const lastMatch = matchedRoutes[matchedRoutes.length - 1];
// If we only match __root__, we ended up not matching any route at all, so
// we fall back to the pathname.
return lastMatch?.routeId !== '__root__' ? lastMatch : undefined;
};

const applyRouteMatch = (
span: NonNullable<ReturnType<typeof startBrowserTracingPageLoadSpan>>,
match: RouteMatch | undefined,
Expand Down
Loading