Skip to content

Commit f1259d9

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

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,37 @@ function checkWebpackPluginOverrides(
379379
* @param isServer Whether or not this function is being called in the context of a server build
380380
* @returns `true` if sentry code should be injected, and `false` otherwise
381381
*/
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(/^pages/, '');
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+
}
388413
}
389414

390415
/**

0 commit comments

Comments
 (0)