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