@@ -379,12 +379,37 @@ function checkWebpackPluginOverrides(
379
379
* @param isServer Whether or not this function is being called in the context of a server build
380
380
* @returns `true` if sentry code should be injected, and `false` otherwise
381
381
*/
382
- function shouldAddSentryToEntryPoint ( entryPointName : string , isServer : boolean ) : boolean {
383
- return (
384
- entryPointName === 'pages/_app' ||
385
- ( entryPointName . includes ( 'pages/api' ) && ! entryPointName . includes ( '_middleware' ) ) ||
386
- ( isServer && entryPointName === 'pages/_error' )
387
- ) ;
382
+ function shouldAddSentryToEntryPoint (
383
+ entryPointName : string ,
384
+ isServer : boolean ,
385
+ ) : boolean {
386
+ // On the server side, by default we inject the `Sentry.init()` code into every page (with a few exceptions).
387
+ if ( isServer ) {
388
+ const entryPointRoute = entryPointName . replace ( / ^ p a g e s / , '' ) ;
389
+ if (
390
+ // All non-API pages contain both of these components, and we don't want to inject more than once, so as long as
391
+ // we're doing the individual pages, it's fine to skip these
392
+ entryPointRoute === '/_app' ||
393
+ entryPointRoute === '/_document' ||
394
+ // While middleware was in beta, it could be anywhere (at any level) in the `pages` directory, and would be called
395
+ // `_middleware.js`. Until the SDK runs successfully in the lambda edge environment, we have to exclude these.
396
+ entryPointName . includes ( '_middleware' ) ||
397
+ // In case anything else weird ends up in there
398
+ ! entryPointName . startsWith ( 'pages/' )
399
+ ) {
400
+ return false ;
401
+ }
402
+
403
+ // We want to inject Sentry into all other pages
404
+ return true ;
405
+ }
406
+
407
+ // On the client side, we only want to inject into `_app`, because that guarantees there'll be only one copy of the
408
+ // SDK in the eventual bundle. Since `_app` is the (effectively) the root component for every nextjs app, inclusing
409
+ // Sentry there means it will be available for every front end page.
410
+ else {
411
+ return entryPointName === 'pages/_app' ;
412
+ }
388
413
}
389
414
390
415
/**
0 commit comments