|
| 1 | +// Inspired from Donnie McNeal's solution: |
| 2 | +// https://gist.github.com/wontondon/e8c4bdf2888875e4c755712e99279536 |
| 3 | + |
| 4 | +import { Transaction, TransactionContext } from '@sentry/types'; |
| 5 | +import { getGlobalObject, logger } from '@sentry/utils'; |
| 6 | +import hoistNonReactStatics from 'hoist-non-react-statics'; |
| 7 | +import React from 'react'; |
| 8 | + |
| 9 | +import { IS_DEBUG_BUILD } from './flags'; |
| 10 | +import { Action, Location } from './types'; |
| 11 | + |
| 12 | +interface RouteObject { |
| 13 | + caseSensitive?: boolean; |
| 14 | + children?: RouteObject[]; |
| 15 | + element?: React.ReactNode; |
| 16 | + index?: boolean; |
| 17 | + path?: string; |
| 18 | +} |
| 19 | + |
| 20 | +type Params<Key extends string = string> = { |
| 21 | + readonly [key in Key]: string | undefined; |
| 22 | +}; |
| 23 | + |
| 24 | +interface RouteMatch<ParamKey extends string = string> { |
| 25 | + params: Params<ParamKey>; |
| 26 | + pathname: string; |
| 27 | + route: RouteObject; |
| 28 | +} |
| 29 | + |
| 30 | +type UseEffect = (cb: () => void, deps: unknown[]) => void; |
| 31 | +type UseLocation = () => Location; |
| 32 | +type UseNavigationType = () => Action; |
| 33 | +type CreateRoutesFromChildren = (children: JSX.Element[]) => RouteObject[]; |
| 34 | +type MatchRoutes = (routes: RouteObject[], location: Location) => RouteMatch[] | null; |
| 35 | + |
| 36 | +let activeTransaction: Transaction | undefined; |
| 37 | + |
| 38 | +let _useEffect: UseEffect; |
| 39 | +let _useLocation: UseLocation; |
| 40 | +let _useNavigationType: UseNavigationType; |
| 41 | +let _createRoutesFromChildren: CreateRoutesFromChildren; |
| 42 | +let _matchRoutes: MatchRoutes; |
| 43 | +let _customStartTransaction: (context: TransactionContext) => Transaction | undefined; |
| 44 | +let _startTransactionOnLocationChange: boolean; |
| 45 | + |
| 46 | +const global = getGlobalObject<Window>(); |
| 47 | + |
| 48 | +const SENTRY_TAGS = { |
| 49 | + 'routing.instrumentation': 'react-router-v6', |
| 50 | +}; |
| 51 | + |
| 52 | +function getInitPathName(): string | undefined { |
| 53 | + if (global && global.location) { |
| 54 | + return global.location.pathname; |
| 55 | + } |
| 56 | + |
| 57 | + return undefined; |
| 58 | +} |
| 59 | + |
| 60 | +export function reactRouterV6Instrumentation( |
| 61 | + useEffect: UseEffect, |
| 62 | + useLocation: UseLocation, |
| 63 | + useNavigationType: UseNavigationType, |
| 64 | + createRoutesFromChildren: CreateRoutesFromChildren, |
| 65 | + matchRoutes: MatchRoutes, |
| 66 | +) { |
| 67 | + return ( |
| 68 | + customStartTransaction: (context: TransactionContext) => Transaction | undefined, |
| 69 | + startTransactionOnPageLoad = true, |
| 70 | + startTransactionOnLocationChange = true, |
| 71 | + ): void => { |
| 72 | + const initPathName = getInitPathName(); |
| 73 | + if (startTransactionOnPageLoad && initPathName) { |
| 74 | + activeTransaction = customStartTransaction({ |
| 75 | + name: initPathName, |
| 76 | + op: 'pageload', |
| 77 | + tags: SENTRY_TAGS, |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + _useEffect = useEffect; |
| 82 | + _useLocation = useLocation; |
| 83 | + _useNavigationType = useNavigationType; |
| 84 | + _matchRoutes = matchRoutes; |
| 85 | + _createRoutesFromChildren = createRoutesFromChildren; |
| 86 | + |
| 87 | + _customStartTransaction = customStartTransaction; |
| 88 | + _startTransactionOnLocationChange = startTransactionOnLocationChange; |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +const getTransactionName = (routes: RouteObject[], location: Location, matchRoutes: MatchRoutes): string => { |
| 93 | + if (!routes || routes.length === 0 || !matchRoutes) { |
| 94 | + return location.pathname; |
| 95 | + } |
| 96 | + |
| 97 | + const branches = matchRoutes(routes, location); |
| 98 | + |
| 99 | + if (branches) { |
| 100 | + // eslint-disable-next-line @typescript-eslint/prefer-for-of |
| 101 | + for (let x = 0; x < branches.length; x++) { |
| 102 | + if (branches[x].route && branches[x].route.path && branches[x].pathname === location.pathname) { |
| 103 | + return branches[x].route.path || location.pathname; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return location.pathname; |
| 109 | +}; |
| 110 | + |
| 111 | +export function withSentryReactRouterV6Routing<P extends Record<string, any>, R extends React.FC<P>>(Routes: R): R { |
| 112 | + if ( |
| 113 | + !_useEffect || |
| 114 | + !_useLocation || |
| 115 | + !_useNavigationType || |
| 116 | + !_createRoutesFromChildren || |
| 117 | + !_matchRoutes || |
| 118 | + !_customStartTransaction |
| 119 | + ) { |
| 120 | + IS_DEBUG_BUILD && |
| 121 | + logger.warn('reactRouterV6Instrumentation was unable to wrap Routes because of one or more missing parameters.'); |
| 122 | + |
| 123 | + return Routes; |
| 124 | + } |
| 125 | + |
| 126 | + let isBaseLocation: boolean = false; |
| 127 | + let routes: RouteObject[]; |
| 128 | + |
| 129 | + const SentryRoutes: React.FC<P> = (props: P) => { |
| 130 | + const location = _useLocation(); |
| 131 | + const navigationType = _useNavigationType(); |
| 132 | + |
| 133 | + _useEffect(() => { |
| 134 | + // Performance concern: |
| 135 | + // This is repeated when <Routes /> is rendered. |
| 136 | + routes = _createRoutesFromChildren(props.children); |
| 137 | + isBaseLocation = true; |
| 138 | + |
| 139 | + if (activeTransaction) { |
| 140 | + activeTransaction.setName(getTransactionName(routes, location, _matchRoutes)); |
| 141 | + } |
| 142 | + |
| 143 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 144 | + }, [props.children]); |
| 145 | + |
| 146 | + _useEffect(() => { |
| 147 | + if (isBaseLocation) { |
| 148 | + if (activeTransaction) { |
| 149 | + activeTransaction.finish(); |
| 150 | + } |
| 151 | + |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + if (_startTransactionOnLocationChange && (navigationType === 'PUSH' || navigationType === 'POP')) { |
| 156 | + if (activeTransaction) { |
| 157 | + activeTransaction.finish(); |
| 158 | + } |
| 159 | + |
| 160 | + activeTransaction = _customStartTransaction({ |
| 161 | + name: getTransactionName(routes, location, _matchRoutes), |
| 162 | + op: 'navigation', |
| 163 | + tags: SENTRY_TAGS, |
| 164 | + }); |
| 165 | + } |
| 166 | + }, [props.children, location, navigationType, isBaseLocation]); |
| 167 | + |
| 168 | + isBaseLocation = false; |
| 169 | + |
| 170 | + // @ts-ignore Setting more specific React Component typing for `R` generic above |
| 171 | + // will break advanced type inference done by react router params |
| 172 | + return <Routes {...props} />; |
| 173 | + }; |
| 174 | + |
| 175 | + hoistNonReactStatics(SentryRoutes, Routes); |
| 176 | + |
| 177 | + // @ts-ignore Setting more specific React Component typing for `R` generic above |
| 178 | + // will break advanced type inference done by react router params |
| 179 | + return SentryRoutes; |
| 180 | +} |
0 commit comments