|
| 1 | +import { |
| 2 | + browserTracingIntegration as originalBrowserTracingIntegration, |
| 3 | + startBrowserTracingNavigationSpan, |
| 4 | + WINDOW, |
| 5 | +} from '@sentry/browser'; |
| 6 | +import { SENTRY_SEGMENT_NAME_SOURCE } from '@sentry/conventions/attributes'; |
| 7 | +import { |
| 8 | + type Client, |
| 9 | + hasSpanStreamingEnabled, |
| 10 | + type Integration, |
| 11 | + NAVIGATION_SPAN_NAME_FALLBACK, |
| 12 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 13 | +} from '@sentry/core'; |
| 14 | + |
| 15 | +type Options = Parameters<typeof originalBrowserTracingIntegration>[0]; |
| 16 | + |
| 17 | +/** |
| 18 | + * Browser tracing for Remix 3. |
| 19 | + * |
| 20 | + * Page loads stay with the upstream integration, because they are ordinary document loads. Navigations |
| 21 | + * do not: `remix/ui` intercepts links and form submissions through the Navigation API and never touches |
| 22 | + * History, so the upstream handler never fires. |
| 23 | + */ |
| 24 | +export function browserTracingIntegration(options: Options = {}): Integration { |
| 25 | + const integration = originalBrowserTracingIntegration({ ...options, instrumentNavigation: false }); |
| 26 | + |
| 27 | + return { |
| 28 | + ...integration, |
| 29 | + afterAllSetup(client) { |
| 30 | + integration.afterAllSetup(client); |
| 31 | + |
| 32 | + if (options.instrumentNavigation !== false) { |
| 33 | + instrumentNavigationApi(client); |
| 34 | + } |
| 35 | + }, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function instrumentNavigationApi(client: Client): void { |
| 40 | + const navigation = (WINDOW as WindowWithNavigation).navigation; |
| 41 | + if (!navigation) { |
| 42 | + // Without the Navigation API the runtime does full document loads, which are page loads already. |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + navigation.addEventListener('navigate', event => { |
| 47 | + const url = event.destination?.url; |
| 48 | + if (!url || !isRuntimeNavigation(event, url)) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + startBrowserTracingNavigationSpan( |
| 53 | + client, |
| 54 | + { |
| 55 | + // Remix 3 gives the browser no route to name this after: `remix/ui` exposes no matched route |
| 56 | + // and never matches client side. Passing the server's pattern down is tracked in (#24872). |
| 57 | + name: hasSpanStreamingEnabled(client) ? NAVIGATION_SPAN_NAME_FALLBACK : pathnameOf(url) || '/', |
| 58 | + attributes: { |
| 59 | + [SENTRY_SEGMENT_NAME_SOURCE]: 'url', |
| 60 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.remix_v3', |
| 61 | + }, |
| 62 | + }, |
| 63 | + // The span needs the destination: `location` still points at the previous page until the |
| 64 | + // navigation finishes. |
| 65 | + { url }, |
| 66 | + ); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Whether the runtime will keep this navigation inside the current document. |
| 72 | + * |
| 73 | + * The same three conditions `startNavigationListener` in `@remix-run/ui` checks before intercepting. |
| 74 | + * What it declines becomes a new document load, which already gets a page load span, so a navigation |
| 75 | + * span here would count the same click twice. |
| 76 | + */ |
| 77 | +function isRuntimeNavigation(event: NavigateEventLike, url: string): boolean { |
| 78 | + // `'remix-document-reload'` is the `info` value the runtime tags its own document reloads with. |
| 79 | + return event.canIntercept && event.info !== 'remix-document-reload' && isSameOrigin(url); |
| 80 | +} |
| 81 | + |
| 82 | +function isSameOrigin(url: string): boolean { |
| 83 | + try { |
| 84 | + return new URL(url).origin === WINDOW.location?.origin; |
| 85 | + } catch { |
| 86 | + return false; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function pathnameOf(url: string): string | undefined { |
| 91 | + try { |
| 92 | + return new URL(url).pathname; |
| 93 | + } catch { |
| 94 | + return undefined; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +interface NavigateEventLike extends Event { |
| 99 | + canIntercept: boolean; |
| 100 | + info?: unknown; |
| 101 | + destination?: { url: string }; |
| 102 | +} |
| 103 | + |
| 104 | +type WindowWithNavigation = typeof WINDOW & { |
| 105 | + navigation?: { addEventListener(type: 'navigate', listener: (event: NavigateEventLike) => void): void }; |
| 106 | +}; |
0 commit comments