@@ -7,6 +7,7 @@ import { createRequire } from 'module';
77import * as path from 'path' ;
88import type { VitePlugin } from './cloudflare' ;
99import { sentryCloudflareNodeWarningPlugin , sentryCloudflareVitePlugin } from './cloudflare' ;
10+ import { makeSentryMiddlewareVitePlugin , SENTRY_MIDDLEWARE_MODULE } from './middleware/virtual' ;
1011import { buildClientSnippet , buildSdkInitFileImportSnippet , buildServerSnippet } from './snippets' ;
1112import { getAstroTunnelOptions , makeTunnelOptionsVitePlugin , TUNNEL_OPTIONS_IMPORT } from './tunnel' ;
1213import type { SentryOptions } from './types' ;
@@ -181,18 +182,23 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
181182 }
182183 }
183184
185+ let serverInitSnippet = '' ;
184186 if ( sdkEnabled . server ) {
185187 const pathToServerInit = serverInitPath ? path . resolve ( serverInitPath ) : findDefaultSdkInitFile ( 'server' ) ;
186188
187189 if ( pathToServerInit ) {
188190 debug && logger . info ( `Using ${ pathToServerInit } for server init.` ) ;
189- // Always inject the server config via `injectScript('page-ssr')`.
190- // This ensures Sentry.init() runs in dev mode (where the Vite plugin doesn't fire)
191- // and also serves as the fallback for non-Cloudflare adapters in production.
192- injectScript ( 'page-ssr' , tunnelOptionsImport + buildSdkInitFileImportSnippet ( pathToServerInit ) ) ;
191+ serverInitSnippet = tunnelOptionsImport + buildSdkInitFileImportSnippet ( pathToServerInit ) ;
193192 } else if ( ! usesCloudflareVitePlugin ) {
194193 debug && logger . info ( 'Using default server init.' ) ;
195- injectScript ( 'page-ssr' , tunnelOptionsImport + buildServerSnippet ( options || { } ) ) ;
194+ serverInitSnippet = tunnelOptionsImport + buildServerSnippet ( options || { } ) ;
195+ }
196+
197+ // Always inject the server config via `injectScript('page-ssr')`.
198+ // This ensures Sentry.init() runs in dev mode (where the Vite plugin doesn't fire)
199+ // and also serves as the fallback for non-Cloudflare adapters in production.
200+ if ( serverInitSnippet ) {
201+ injectScript ( 'page-ssr' , serverInitSnippet ) ;
196202 }
197203
198204 if ( isCloudflareWorkers && command !== 'dev' ) {
@@ -239,9 +245,10 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
239245 const shouldAddMiddleware = sdkEnabled . server && autoInstrumentation ?. requestHandler !== false ;
240246
241247 if ( isSSR && shouldAddMiddleware ) {
248+ updateConfig ( { vite : { plugins : [ makeSentryMiddlewareVitePlugin ( serverInitSnippet ) ] } } ) ;
242249 addMiddleware ( {
243250 order : 'pre' ,
244- entrypoint : '@sentry/astro/middleware' ,
251+ entrypoint : SENTRY_MIDDLEWARE_MODULE ,
245252 } ) ;
246253 }
247254 } ,
0 commit comments