Skip to content

Commit 95c6732

Browse files
committed
.
1 parent 06ac632 commit 95c6732

3 files changed

Lines changed: 53 additions & 7 deletions

File tree

dev-packages/e2e-tests/test-applications/sveltekit-3/tests/errors.server.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import { expect, test } from '@playwright/test';
22
import { waitForError } from '@sentry-internal/test-utils';
33

44
test.describe('server-side errors', () => {
5-
// FIXME(sveltekit-3): the universal load function's frame is reported as `load$1` (not `load`)
6-
// because the SDK still wraps universal `+page.ts` load in the server build. Unlike server-only
7-
// load, this isn't suppressed by native-tracing detection: the wrapper is skipped via
8-
// `config.build.ssr`, which is unreliable under Vite 8's Environment API. Unskip once the SDK
9-
// detects the server environment via the Vite Environment API.
10-
test.skip('captures universal load error', async ({ page }) => {
5+
test('captures universal load error', async ({ page }) => {
116
const errorEventPromise = waitForError('sveltekit-3', errorEvent => {
127
return errorEvent?.exception?.values?.[0]?.value === 'Universal Load Error (server)';
138
});

packages/sveltekit/src/vite/autoInstrument.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ export function makeAutoInstrumentationPlugin(options: AutoInstrumentPluginOptio
7575
},
7676

7777
async load(id) {
78-
if (onlyInstrumentClient && isServerBuild) {
78+
// On Vite 6+ `config.build.ssr` captured in `configResolved` no longer reliably reflects the per-environment build.
79+
// Prefer the environment of the current build (`this.environment.name === 'ssr'`) and fall back to
80+
// `isServerBuild` for older Vite versions that don't expose environments.
81+
const environmentName = (this as { environment?: { name?: string } }).environment?.name;
82+
const isServerEnvironment = environmentName != null ? environmentName === 'ssr' : !!isServerBuild;
83+
84+
if (onlyInstrumentClient && isServerEnvironment) {
7985
return null;
8086
}
8187

packages/sveltekit/test/vite/autoInstrument.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,51 @@ describe('makeAutoInstrumentationPlugin()', () => {
280280
);
281281
});
282282
});
283+
284+
describe('when the server build is detected via the Vite Environment API', () => {
285+
// On Vite 6+ `config.build.ssr` no longer reliably reflects the per-environment
286+
// build, so the plugin relies on the current environment (`this.environment.name`). When
287+
// `onlyInstrumentClient` is `true`, universal load must not be wrapped in the `ssr` environment
288+
// (but should still be wrapped in `client`), even when `config.build.ssr`/`configResolved`
289+
// didn't flag a server build.
290+
it.each(['path/to/+page.ts', 'path/to/+layout.js', 'path/to/+page.server.ts'])(
291+
"doesn't wrap %s in the `ssr` environment",
292+
async (path: string) => {
293+
const plugin = makeAutoInstrumentationPlugin({
294+
debug: false,
295+
load: true,
296+
serverLoad: true,
297+
onlyInstrumentClient: true,
298+
});
299+
300+
// `configResolved` is intentionally not called - `isServerBuild` stays `undefined`
301+
// @ts-expect-error this exists and is callable; bind `this.environment` like Vite does
302+
const loadResult = await plugin.load.call({ environment: { name: 'ssr' } }, path);
303+
304+
expect(loadResult).toEqual(null);
305+
},
306+
);
307+
308+
it('still wraps universal load in the `client` environment', async () => {
309+
const plugin = makeAutoInstrumentationPlugin({
310+
debug: false,
311+
load: true,
312+
serverLoad: true,
313+
onlyInstrumentClient: true,
314+
});
315+
316+
const path = 'path/to/+page.ts';
317+
// @ts-expect-error this exists and is callable; bind `this.environment` like Vite does
318+
const loadResult = await plugin.load.call({ environment: { name: 'client' } }, path);
319+
320+
expect(loadResult).toBe(
321+
'import { wrapLoadWithSentry } from "@sentry/sveltekit";' +
322+
`import * as userModule from "${path}?sentry-auto-wrap";` +
323+
'export const load = userModule.load ? wrapLoadWithSentry(userModule.load) : undefined;' +
324+
`export * from "${path}?sentry-auto-wrap";`,
325+
);
326+
});
327+
});
283328
});
284329

285330
describe('canWrapLoad', () => {

0 commit comments

Comments
 (0)