Skip to content

Commit a0bc030

Browse files
committed
try cached dynamic import in handler
1 parent 83d9f6f commit a0bc030

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

packages/sveltekit/src/server/handle.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ export function addSentryCodeToPage(options: { injectFetchProxyScript: boolean }
9393
* ```
9494
*/
9595
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,
100100
};
101101

102102
const sentryRequestHandler: Handle = input => {
@@ -131,12 +131,24 @@ export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
131131

132132
async 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

Comments
 (0)