Skip to content

Commit

Permalink
inject sentry into all serverside entrypoints besides _app and `_do…
Browse files Browse the repository at this point in the history
…cument`
  • Loading branch information
lobsterkatie committed Nov 15, 2022
1 parent 5d5d1e9 commit f1259d9
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions packages/nextjs/src/config/webpack.ts
Expand Up @@ -379,12 +379,37 @@ function checkWebpackPluginOverrides(
* @param isServer Whether or not this function is being called in the context of a server build
* @returns `true` if sentry code should be injected, and `false` otherwise
*/
function shouldAddSentryToEntryPoint(entryPointName: string, isServer: boolean): boolean {
return (
entryPointName === 'pages/_app' ||
(entryPointName.includes('pages/api') && !entryPointName.includes('_middleware')) ||
(isServer && entryPointName === 'pages/_error')
);
function shouldAddSentryToEntryPoint(
entryPointName: string,
isServer: boolean,
): boolean {
// On the server side, by default we inject the `Sentry.init()` code into every page (with a few exceptions).
if (isServer) {
const entryPointRoute = entryPointName.replace(/^pages/, '');
if (
// All non-API pages contain both of these components, and we don't want to inject more than once, so as long as
// we're doing the individual pages, it's fine to skip these
entryPointRoute === '/_app' ||
entryPointRoute === '/_document' ||
// While middleware was in beta, it could be anywhere (at any level) in the `pages` directory, and would be called
// `_middleware.js`. Until the SDK runs successfully in the lambda edge environment, we have to exclude these.
entryPointName.includes('_middleware') ||
// In case anything else weird ends up in there
!entryPointName.startsWith('pages/')
) {
return false;
}

// We want to inject Sentry into all other pages
return true;
}

// On the client side, we only want to inject into `_app`, because that guarantees there'll be only one copy of the
// SDK in the eventual bundle. Since `_app` is the (effectively) the root component for every nextjs app, inclusing
// Sentry there means it will be available for every front end page.
else {
return entryPointName === 'pages/_app';
}
}

/**
Expand Down

0 comments on commit f1259d9

Please sign in to comment.