@@ -93,10 +93,10 @@ export function addSentryCodeToPage(options: { injectFetchProxyScript: boolean }
93
93
* ```
94
94
*/
95
95
export function sentryHandle ( handlerOptions ?: SentryHandleOptions ) : Handle {
96
- const options : Required < SentryHandleOptions > = {
97
- handleUnknownRoutes : false ,
98
- injectFetchProxyScript : isFetchProxyRequired ( '2.16.0' ) ,
99
- ...handlerOptions ,
96
+ const { handleUnknownRoutes , ... rest } = handlerOptions ?? { } ;
97
+ const options = {
98
+ handleUnknownRoutes : handleUnknownRoutes ?? false ,
99
+ ...rest ,
100
100
} ;
101
101
102
102
const sentryRequestHandler : Handle = input => {
@@ -131,12 +131,24 @@ export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
131
131
132
132
async function instrumentHandle (
133
133
{ event, resolve } : Parameters < Handle > [ 0 ] ,
134
- options : Required < SentryHandleOptions > ,
134
+ options : SentryHandleOptions ,
135
135
) : Promise < Response > {
136
136
if ( ! event . route ?. id && ! options . handleUnknownRoutes ) {
137
137
return resolve ( event ) ;
138
138
}
139
139
140
+ // caching the result of the version check in `options.injectFetchProxyScript`
141
+ // to avoid doing the dynamic import on every request
142
+ if ( options . injectFetchProxyScript == null ) {
143
+ try {
144
+ // @ts -expect-error - the dynamic import is fine here
145
+ const { VERSION } = await import ( '@sveltejs/kit' ) ;
146
+ options . injectFetchProxyScript = isFetchProxyRequired ( VERSION ) ;
147
+ } catch {
148
+ options . injectFetchProxyScript = true ;
149
+ }
150
+ }
151
+
140
152
const routeName = `${ event . request . method } ${ event . route ?. id || event . url . pathname } ` ;
141
153
142
154
if ( getIsolationScope ( ) !== getDefaultIsolationScope ( ) ) {
@@ -161,7 +173,7 @@ async function instrumentHandle(
161
173
normalizedRequest : winterCGRequestToRequestData ( event . request . clone ( ) ) ,
162
174
} ) ;
163
175
const res = await resolve ( event , {
164
- transformPageChunk : addSentryCodeToPage ( { injectFetchProxyScript : options . injectFetchProxyScript } ) ,
176
+ transformPageChunk : addSentryCodeToPage ( { injectFetchProxyScript : options . injectFetchProxyScript ?? true } ) ,
165
177
} ) ;
166
178
if ( span ) {
167
179
setHttpStatus ( span , res . status ) ;
0 commit comments