Skip to content

Commit 01acf5c

Browse files
committed
feat(sveltekit): Only inject fetch proxy script for SvelteKit < 2.16.0
1 parent a811947 commit 01acf5c

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

packages/sveltekit/src/server/handle.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
winterCGRequestToRequestData,
1515
withIsolationScope,
1616
} from '@sentry/core';
17-
import type { Handle, ResolveOptions } from '@sveltejs/kit';
17+
import { VERSION, type Handle, type ResolveOptions } from '@sveltejs/kit';
1818

1919
import { DEBUG_BUILD } from '../common/debug-build';
2020
import { flushIfServerless, getTracePropagationData, sendErrorToSentry } from './utils';
@@ -95,7 +95,7 @@ export function addSentryCodeToPage(options: { injectFetchProxyScript: boolean }
9595
export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
9696
const options: Required<SentryHandleOptions> = {
9797
handleUnknownRoutes: false,
98-
injectFetchProxyScript: true,
98+
injectFetchProxyScript: isFetchProxyRequired(VERSION),
9999
...handlerOptions,
100100
};
101101

@@ -177,3 +177,19 @@ async function instrumentHandle(
177177
await flushIfServerless();
178178
}
179179
}
180+
181+
/**
182+
* We only need to inject the fetch proxy script for SvelteKit versions < 2.16.0.
183+
* Exported only for testing.
184+
*/
185+
export function isFetchProxyRequired(version: string): boolean {
186+
try {
187+
const [major, minor] = version.trim().replace(/-.*/, '').split('.').map(Number);
188+
if (major != null && minor != null && (major > 2 || (major === 2 && minor >= 16))) {
189+
return false;
190+
}
191+
} catch {
192+
// ignore
193+
}
194+
return true;
195+
}

packages/sveltekit/test/server/handle.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { Handle } from '@sveltejs/kit';
1414
import { redirect } from '@sveltejs/kit';
1515
import { vi } from 'vitest';
1616

17-
import { FETCH_PROXY_SCRIPT, addSentryCodeToPage, sentryHandle } from '../../src/server/handle';
17+
import { FETCH_PROXY_SCRIPT, addSentryCodeToPage, isFetchProxyRequired, sentryHandle } from '../../src/server/handle';
1818
import { getDefaultNodeClientOptions } from '../utils';
1919

2020
const mockCaptureException = vi.spyOn(SentryNode, 'captureException').mockImplementation(() => 'xx');
@@ -462,3 +462,20 @@ describe('addSentryCodeToPage', () => {
462462
expect(transformed).not.toContain(`<script >${FETCH_PROXY_SCRIPT}</script>`);
463463
});
464464
});
465+
466+
describe('isFetchProxyRequired', () => {
467+
it.each(['2.16.0', '2.16.1', '2.17.0', '3.0.0', '3.0.0-alpha.0'])(
468+
'returns false if the version is greater than or equal to 2.16.0 (%s)',
469+
version => {
470+
expect(isFetchProxyRequired(version)).toBe(false);
471+
},
472+
);
473+
474+
it.each(['2.15.0', '2.15.1', '1.30.0', '1.0.0'])('returns true if the version is lower than 2.16.0 (%s)', version => {
475+
expect(isFetchProxyRequired(version)).toBe(true);
476+
});
477+
478+
it.each(['invalid', 'a.b.c'])('returns true for an invalid version (%s)', version => {
479+
expect(isFetchProxyRequired(version)).toBe(true);
480+
});
481+
});

0 commit comments

Comments
 (0)