|
| 1 | +import { Transaction, TransactionContext } from '@sentry/types'; |
| 2 | +import { getGlobalObject, logger } from '@sentry/utils'; |
| 3 | +import * as React from 'react'; |
| 4 | + |
| 5 | +import { IS_DEBUG_BUILD } from '../flags'; |
| 6 | + |
| 7 | +const DEFAULT_TAGS = { |
| 8 | + 'routing.instrumentation': 'remix-router', |
| 9 | +} as const; |
| 10 | + |
| 11 | +type Params<Key extends string = string> = { |
| 12 | + readonly [key in Key]: string | undefined; |
| 13 | +}; |
| 14 | + |
| 15 | +interface RouteMatch<ParamKey extends string = string> { |
| 16 | + params: Params<ParamKey>; |
| 17 | + pathname: string; |
| 18 | + id: string; |
| 19 | + handle: unknown; |
| 20 | +} |
| 21 | + |
| 22 | +type UseEffect = (cb: () => void, deps: unknown[]) => void; |
| 23 | +type UseLocation = () => { |
| 24 | + pathname: string; |
| 25 | + search?: string; |
| 26 | + hash?: string; |
| 27 | + state?: unknown; |
| 28 | + key?: unknown; |
| 29 | +}; |
| 30 | +type UseMatches = () => RouteMatch[] | null; |
| 31 | + |
| 32 | +let activeTransaction: Transaction | undefined; |
| 33 | + |
| 34 | +let _useEffect: UseEffect; |
| 35 | +let _useLocation: UseLocation; |
| 36 | +let _useMatches: UseMatches; |
| 37 | + |
| 38 | +let _customStartTransaction: (context: TransactionContext) => Transaction | undefined; |
| 39 | +let _startTransactionOnLocationChange: boolean; |
| 40 | + |
| 41 | +const global = getGlobalObject<Window>(); |
| 42 | + |
| 43 | +function getInitPathName(): string | undefined { |
| 44 | + if (global && global.location) { |
| 45 | + return global.location.pathname; |
| 46 | + } |
| 47 | + |
| 48 | + return undefined; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Creates a react-router v6 instrumention for Remix applications. |
| 53 | + * |
| 54 | + * This implementation is slightly different (and simpler) from the react-router instrumentation |
| 55 | + * as in Remix, `useMatches` hook is available where in react-router-v6 it's not yet. |
| 56 | + */ |
| 57 | +export function remixRouterInstrumentation(useEffect: UseEffect, useLocation: UseLocation, useMatches: UseMatches) { |
| 58 | + return ( |
| 59 | + customStartTransaction: (context: TransactionContext) => Transaction | undefined, |
| 60 | + startTransactionOnPageLoad = true, |
| 61 | + startTransactionOnLocationChange = true, |
| 62 | + ): void => { |
| 63 | + const initPathName = getInitPathName(); |
| 64 | + if (startTransactionOnPageLoad && initPathName) { |
| 65 | + activeTransaction = customStartTransaction({ |
| 66 | + name: initPathName, |
| 67 | + op: 'pageload', |
| 68 | + tags: DEFAULT_TAGS, |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + _useEffect = useEffect; |
| 73 | + _useLocation = useLocation; |
| 74 | + _useMatches = useMatches; |
| 75 | + |
| 76 | + _customStartTransaction = customStartTransaction; |
| 77 | + _startTransactionOnLocationChange = startTransactionOnLocationChange; |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Wraps a remix `root` (see: https://remix.run/docs/en/v1/guides/migrating-react-router-app#creating-the-root-route) |
| 83 | + * To enable pageload/navigation tracing on every route. |
| 84 | + */ |
| 85 | +export function withSentryRouteTracing<P extends Record<string, unknown>, R extends React.FC<P>>(OrigApp: R): R { |
| 86 | + // Early return when any of the required functions is not available. |
| 87 | + if (!_useEffect || !_useLocation || !_useMatches || !_customStartTransaction) { |
| 88 | + IS_DEBUG_BUILD && logger.warn('Remix SDK was unable to wrap your root because of one or more missing parameters.'); |
| 89 | + |
| 90 | + // @ts-ignore Setting more specific React Component typing for `R` generic above |
| 91 | + // will break advanced type inference done by react router params |
| 92 | + return OrigApp; |
| 93 | + } |
| 94 | + |
| 95 | + const SentryRoot: React.FC<P> = (props: P) => { |
| 96 | + let isBaseLocation: boolean = false; |
| 97 | + |
| 98 | + const location = _useLocation(); |
| 99 | + const matches = _useMatches(); |
| 100 | + |
| 101 | + _useEffect(() => { |
| 102 | + if (activeTransaction && matches && matches.length) { |
| 103 | + activeTransaction.setName(matches[matches.length - 1].id); |
| 104 | + } |
| 105 | + |
| 106 | + isBaseLocation = true; |
| 107 | + }, []); |
| 108 | + |
| 109 | + _useEffect(() => { |
| 110 | + if (isBaseLocation) { |
| 111 | + if (activeTransaction) { |
| 112 | + activeTransaction.finish(); |
| 113 | + } |
| 114 | + |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (_startTransactionOnLocationChange && matches && matches.length) { |
| 119 | + if (activeTransaction) { |
| 120 | + activeTransaction.finish(); |
| 121 | + } |
| 122 | + |
| 123 | + activeTransaction = _customStartTransaction({ |
| 124 | + name: matches[matches.length - 1].id, |
| 125 | + op: 'navigation', |
| 126 | + tags: DEFAULT_TAGS, |
| 127 | + }); |
| 128 | + } |
| 129 | + }, [location]); |
| 130 | + |
| 131 | + isBaseLocation = false; |
| 132 | + |
| 133 | + // @ts-ignore Setting more specific React Component typing for `R` generic above |
| 134 | + // will break advanced type inference done by react router params |
| 135 | + return <OrigApp {...props} />; |
| 136 | + }; |
| 137 | + |
| 138 | + // @ts-ignore Setting more specific React Component typing for `R` generic above |
| 139 | + // will break advanced type inference done by react router params |
| 140 | + return SentryRoot; |
| 141 | +} |
0 commit comments