Skip to content

Commit c3c5495

Browse files
committed
inject sentry into all serverside entrypoints besides _app and _document
1 parent 58afac0 commit c3c5495

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

packages/nextjs/src/config/webpack.ts

+31-5
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,37 @@ 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. (Note: Even if a given user doesn't have either or
389+
// both of these in their `pages/` folder, they'll exist as entrypoints because nextjs will supply default
390+
// versions.)
391+
entryPointRoute === '/_app' ||
392+
entryPointRoute === '/_document' ||
393+
// While middleware was in beta, it could be anywhere (at any level) in the `pages` directory, and would be called
394+
// `_middleware.js`. Until the SDK runs successfully in the lambda edge environment, we have to exclude these.
395+
entryPointName.includes('_middleware') ||
396+
// Newer versions of nextjs are starting to introduce things outside the `pages/` folder (middleware, an `app/`
397+
// directory, etc), but until those features are stable and we know how we want to support them, the safest bet is
398+
// not to inject anywhere but inside `pages/`.
399+
!entryPointName.startsWith('pages/')
400+
) {
401+
return false;
402+
}
403+
404+
// We want to inject Sentry into all other pages
405+
return true;
406+
}
407+
408+
// On the client side, we only want to inject into `_app`, because that guarantees there'll be only one copy of the
409+
// SDK in the eventual bundle. Since `_app` is the (effectively) the root component for every nextjs app, inclusing
410+
// Sentry there means it will be available for every front end page.
411+
else {
412+
return entryPointName === 'pages/_app';
413+
}
388414
}
389415

390416
/**

0 commit comments

Comments
 (0)