Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { expect, test } from '@playwright/test';
import { waitForError } from '@sentry-internal/test-utils';

test.describe('server-side errors', () => {
// FIXME(sveltekit-3): the universal load function's frame is reported as `load$1` (not `load`)
// because the SDK still wraps universal `+page.ts` load in the server build. Unlike server-only
// load, this isn't suppressed by native-tracing detection: the wrapper is skipped via
// `config.build.ssr`, which is unreliable under Vite 8's Environment API. Unskip once the SDK
// detects the server environment via the Vite Environment API.
test.skip('captures universal load error', async ({ page }) => {
test('captures universal load error', async ({ page }) => {
const errorEventPromise = waitForError('sveltekit-3', errorEvent => {
return errorEvent?.exception?.values?.[0]?.value === 'Universal Load Error (server)';
});
Expand Down
8 changes: 7 additions & 1 deletion packages/sveltekit/src/vite/autoInstrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ export function makeAutoInstrumentationPlugin(options: AutoInstrumentPluginOptio
},

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

if (onlyInstrumentClient && isServerEnvironment) {
return null;
}

Expand Down
45 changes: 45 additions & 0 deletions packages/sveltekit/test/vite/autoInstrument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,51 @@ describe('makeAutoInstrumentationPlugin()', () => {
);
});
});

describe('when the server build is detected via the Vite Environment API', () => {
// On Vite 6+ `config.build.ssr` no longer reliably reflects the per-environment
// build, so the plugin relies on the current environment (`this.environment.name`). When
// `onlyInstrumentClient` is `true`, universal load must not be wrapped in the `ssr` environment
// (but should still be wrapped in `client`), even when `config.build.ssr`/`configResolved`
// didn't flag a server build.
it.each(['path/to/+page.ts', 'path/to/+layout.js', 'path/to/+page.server.ts'])(
"doesn't wrap %s in the `ssr` environment",
async (path: string) => {
const plugin = makeAutoInstrumentationPlugin({
debug: false,
load: true,
serverLoad: true,
onlyInstrumentClient: true,
});

// `configResolved` is intentionally not called - `isServerBuild` stays `undefined`
// @ts-expect-error this exists and is callable; bind `this.environment` like Vite does
const loadResult = await plugin.load.call({ environment: { name: 'ssr' } }, path);

expect(loadResult).toEqual(null);
},
);

it('still wraps universal load in the `client` environment', async () => {
const plugin = makeAutoInstrumentationPlugin({
debug: false,
load: true,
serverLoad: true,
onlyInstrumentClient: true,
});

const path = 'path/to/+page.ts';
// @ts-expect-error this exists and is callable; bind `this.environment` like Vite does
const loadResult = await plugin.load.call({ environment: { name: 'client' } }, path);

expect(loadResult).toBe(
'import { wrapLoadWithSentry } from "@sentry/sveltekit";' +
`import * as userModule from "${path}?sentry-auto-wrap";` +
'export const load = userModule.load ? wrapLoadWithSentry(userModule.load) : undefined;' +
`export * from "${path}?sentry-auto-wrap";`,
);
});
});
});

describe('canWrapLoad', () => {
Expand Down
Loading