Skip to content

Commit af132f5

Browse files
committed
.
1 parent 30c37ee commit af132f5

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
@@ -293,6 +293,51 @@ describe('makeAutoInstrumentationPlugin()', () => {
293293
});
294294
});
295295
});
296+
297+
describe('when the server build is detected via the Vite Environment API', () => {
298+
// On Vite 6+ `config.build.ssr` no longer reliably reflects the per-environment
299+
// build, so the plugin relies on the current environment (`this.environment.name`). When
300+
// `onlyInstrumentClient` is `true`, universal load must not be wrapped in the `ssr` environment
301+
// (but should still be wrapped in `client`), even when `config.build.ssr`/`configResolved`
302+
// didn't flag a server build.
303+
it.each(['path/to/+page.ts', 'path/to/+layout.js', 'path/to/+page.server.ts'])(
304+
"doesn't wrap %s in the `ssr` environment",
305+
async (path: string) => {
306+
const plugin = makeAutoInstrumentationPlugin({
307+
debug: false,
308+
load: true,
309+
serverLoad: true,
310+
onlyInstrumentClient: true,
311+
});
312+
313+
// `configResolved` is intentionally not called - `isServerBuild` stays `undefined`
314+
// @ts-expect-error this exists and is callable; bind `this.environment` like Vite does
315+
const loadResult = await plugin.load.call({ environment: { name: 'ssr' } }, path);
316+
317+
expect(loadResult).toEqual(null);
318+
},
319+
);
320+
321+
it('still wraps universal load in the `client` environment', async () => {
322+
const plugin = makeAutoInstrumentationPlugin({
323+
debug: false,
324+
load: true,
325+
serverLoad: true,
326+
onlyInstrumentClient: true,
327+
});
328+
329+
const path = 'path/to/+page.ts';
330+
// @ts-expect-error this exists and is callable; bind `this.environment` like Vite does
331+
const loadResult = await plugin.load.call({ environment: { name: 'client' } }, path);
332+
333+
expect(loadResult).toBe(
334+
'import { wrapLoadWithSentry } from "@sentry/sveltekit";' +
335+
`import * as userModule from "${path}?sentry-auto-wrap";` +
336+
'export const load = userModule.load ? wrapLoadWithSentry(userModule.load) : undefined;' +
337+
`export * from "${path}?sentry-auto-wrap";`,
338+
);
339+
});
340+
});
296341
});
297342

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

0 commit comments

Comments
 (0)