@@ -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
298343describe ( 'canWrapLoad' , ( ) => {
0 commit comments