Skip to content

Commit f21774d

Browse files
committed
inject sentry into all serverside entrypoints besides _app and _document
1 parent 5d5d1e9 commit f21774d

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

packages/nextjs/src/config/webpack.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,33 @@ function checkWebpackPluginOverrides(
380380
* @returns `true` if sentry code should be injected, and `false` otherwise
381381
*/
382382
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-
);
383+
// On the server side, by default we inject the `Sentry.init()` code into every page (with a few exceptions).
384+
if (isServer) {
385+
const entryPointRoute = entryPointName.replace(/^pages/, '');
386+
if (
387+
// All non-API pages contain both of these components, and we don't want to inject more than once, so as long as
388+
// we're doing the individual pages, it's fine to skip these
389+
entryPointRoute === '/_app' ||
390+
entryPointRoute === '/_document' ||
391+
// While middleware was in beta, it could be anywhere (at any level) in the `pages` directory, and would be called
392+
// `_middleware.js`. Until the SDK runs successfully in the lambda edge environment, we have to exclude these.
393+
entryPointName.includes('_middleware') ||
394+
// In case anything else weird ends up in there
395+
!entryPointName.startsWith('pages/')
396+
) {
397+
return false;
398+
}
399+
400+
// We want to inject Sentry into all other pages
401+
return true;
402+
}
403+
404+
// On the client side, we only want to inject into `_app`, because that guarantees there'll be only one copy of the
405+
// SDK in the eventual bundle. Since `_app` is the (effectively) the root component for every nextjs app, inclusing
406+
// Sentry there means it will be available for every front end page.
407+
else {
408+
return entryPointName === 'pages/_app';
409+
}
388410
}
389411

390412
/**

0 commit comments

Comments
 (0)