@sentry/nextjs
Version:
Official Sentry SDK for Next.js
41 lines (38 loc) • 1.79 kB
JavaScript
import { stripUrlQueryAndFragment, NAVIGATION_SPAN_NAME_FALLBACK, hasSpanStreamingEnabled } from '@sentry/core';
import { startBrowserTracingNavigationSpan, getAbsoluteUrl } from '@sentry/react';
import RouterImport from 'next/router';
import { URL_TEMPLATE, SENTRY_SEGMENT_NAME_SOURCE, SENTRY_ORIGIN, SENTRY_OP } from '@sentry/conventions/attributes';
import { NAVIGATION } from '@sentry/conventions/op';
import { getNextRouteFromPathname } from './pagesRouterRoutingInstrumentation.js';
const Router = RouterImport.events ? RouterImport : RouterImport.default;
function pagesRouterInstrumentNavigation(client) {
Router.events.on("routeChangeStart", (navigationTarget) => {
const strippedNavigationTarget = stripUrlQueryAndFragment(navigationTarget);
const matchedRoute = getNextRouteFromPathname(strippedNavigationTarget);
let newLocation;
let spanSource;
if (matchedRoute) {
newLocation = matchedRoute;
spanSource = "route";
} else {
newLocation = strippedNavigationTarget;
spanSource = "url";
}
startBrowserTracingNavigationSpan(
client,
{
// With span streaming, span names have to be low cardinality, so we can't fall back to the URL.
name: spanSource === "route" || !hasSpanStreamingEnabled(client) ? newLocation : NAVIGATION_SPAN_NAME_FALLBACK,
attributes: {
[SENTRY_OP]: NAVIGATION,
[SENTRY_ORIGIN]: "auto.navigation.nextjs.pages_router_instrumentation",
[SENTRY_SEGMENT_NAME_SOURCE]: spanSource,
...spanSource === "route" && { [URL_TEMPLATE]: newLocation }
}
},
{ url: getAbsoluteUrl(navigationTarget) }
);
});
}
export { pagesRouterInstrumentNavigation };
//# sourceMappingURL=pagesRouterNavigationInstrumentation.js.map