1
+ /* eslint-disable max-lines */
1
2
import { captureException , getCurrentHub } from '@sentry/node' ;
2
3
import { getActiveTransaction } from '@sentry/tracing' ;
3
- import { addExceptionMechanism , fill , loadModule , logger , stripUrlQueryAndFragment } from '@sentry/utils' ;
4
+ import {
5
+ addExceptionMechanism ,
6
+ fill ,
7
+ loadModule ,
8
+ logger ,
9
+ serializeBaggage ,
10
+ stripUrlQueryAndFragment ,
11
+ } from '@sentry/utils' ;
4
12
5
13
// Types vendored from @remix -run/[email protected] :
6
14
// https://github.com/remix-run/remix/blob/f3691d51027b93caa3fd2cdfe146d7b62a6eb8f2/packages/remix-server-runtime/server.ts
@@ -20,11 +28,26 @@ interface Route {
20
28
parentId ?: string ;
21
29
path ?: string ;
22
30
}
31
+ interface RouteData {
32
+ [ routeId : string ] : AppData ;
33
+ }
34
+
35
+ interface MetaFunction {
36
+ ( args : { data : AppData ; parentsData : RouteData ; params : Params ; location : Location } ) : HtmlMetaDescriptor ;
37
+ }
38
+
39
+ interface HtmlMetaDescriptor {
40
+ [ name : string ] : null | string | undefined | Record < string , string > | Array < Record < string , string > | string > ;
41
+ charset ?: 'utf-8' ;
42
+ charSet ?: 'utf-8' ;
43
+ title ?: string ;
44
+ }
23
45
24
46
interface ServerRouteModule {
25
47
action ?: DataFunction ;
26
48
headers ?: unknown ;
27
49
loader ?: DataFunction ;
50
+ meta ?: MetaFunction | HtmlMetaDescriptor ;
28
51
}
29
52
30
53
interface ServerRoute extends Route {
@@ -209,6 +232,36 @@ function makeWrappedLoader(origAction: DataFunction): DataFunction {
209
232
return makeWrappedDataFunction ( origAction , 'loader' ) ;
210
233
}
211
234
235
+ function makeWrappedMeta ( origMeta : MetaFunction | HtmlMetaDescriptor = { } ) : MetaFunction {
236
+ return function (
237
+ this : unknown ,
238
+ args : { data : AppData ; parentsData : RouteData ; params : Params ; location : Location } ,
239
+ ) : HtmlMetaDescriptor {
240
+ let origMetaResult ;
241
+ if ( origMeta instanceof Function ) {
242
+ origMetaResult = origMeta . call ( this , args ) ;
243
+ } else {
244
+ origMetaResult = origMeta ;
245
+ }
246
+
247
+ const scope = getCurrentHub ( ) . getScope ( ) ;
248
+ if ( scope ) {
249
+ const span = scope . getSpan ( ) ;
250
+ const transaction = getActiveTransaction ( ) ;
251
+
252
+ if ( span && transaction ) {
253
+ return {
254
+ ...origMetaResult ,
255
+ 'sentry-trace' : span . toTraceparent ( ) ,
256
+ baggage : serializeBaggage ( transaction . getBaggage ( ) ) ,
257
+ } ;
258
+ }
259
+ }
260
+
261
+ return origMetaResult ;
262
+ } ;
263
+ }
264
+
212
265
function wrapRequestHandler ( origRequestHandler : RequestHandler ) : RequestHandler {
213
266
return async function ( this : unknown , request : Request , loadContext ?: unknown ) : Promise < Response > {
214
267
const hub = getCurrentHub ( ) ;
@@ -250,6 +303,8 @@ function makeWrappedCreateRequestHandler(
250
303
for ( const [ id , route ] of Object . entries ( build . routes ) ) {
251
304
const wrappedRoute = { ...route , module : { ...route . module } } ;
252
305
306
+ fill ( wrappedRoute . module , 'meta' , makeWrappedMeta ) ;
307
+
253
308
if ( wrappedRoute . module . action ) {
254
309
fill ( wrappedRoute . module , 'action' , makeWrappedAction ) ;
255
310
}
0 commit comments