@@ -93,10 +93,10 @@ export function addSentryCodeToPage(options: { injectFetchProxyScript: boolean }
9393 * ```
9494 */
9595export 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 ,
100100 } ;
101101
102102 const sentryRequestHandler : Handle = input => {
@@ -131,12 +131,24 @@ export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
131131
132132async function instrumentHandle (
133133 { event, resolve } : Parameters < Handle > [ 0 ] ,
134- options : Required < SentryHandleOptions > ,
134+ options : SentryHandleOptions ,
135135) : Promise < Response > {
136136 if ( ! event . route ?. id && ! options . handleUnknownRoutes ) {
137137 return resolve ( event ) ;
138138 }
139139
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+
140152 const routeName = `${ event . request . method } ${ event . route ?. id || event . url . pathname } ` ;
141153
142154 if ( getIsolationScope ( ) !== getDefaultIsolationScope ( ) ) {
@@ -161,7 +173,7 @@ async function instrumentHandle(
161173 normalizedRequest : winterCGRequestToRequestData ( event . request . clone ( ) ) ,
162174 } ) ;
163175 const res = await resolve ( event , {
164- transformPageChunk : addSentryCodeToPage ( { injectFetchProxyScript : options . injectFetchProxyScript } ) ,
176+ transformPageChunk : addSentryCodeToPage ( { injectFetchProxyScript : options . injectFetchProxyScript ?? true } ) ,
165177 } ) ;
166178 if ( span ) {
167179 setHttpStatus ( span , res . status ) ;
0 commit comments