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