Skip to content

Commit 87893a9

Browse files
authored
fix(next): Inject SDK in dev mode (#6368)
1 parent 8e94898 commit 87893a9

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ async function addSentryToEntryProperty(
290290
// we know is that it won't have gotten *simpler* in form, so we only need to worry about the object and function
291291
// options. See https://webpack.js.org/configuration/entry-context/#entry.
292292

293-
const { isServer, dir: projectDir } = buildContext;
293+
const { isServer, dir: projectDir, dev: isDev } = buildContext;
294294

295295
const newEntryProperty =
296296
typeof currentEntryProperty === 'function' ? await currentEntryProperty() : { ...currentEntryProperty };
@@ -303,7 +303,7 @@ async function addSentryToEntryProperty(
303303

304304
// inject into all entry points which might contain user's code
305305
for (const entryPointName in newEntryProperty) {
306-
if (shouldAddSentryToEntryPoint(entryPointName, isServer, userSentryOptions.excludeServerRoutes)) {
306+
if (shouldAddSentryToEntryPoint(entryPointName, isServer, userSentryOptions.excludeServerRoutes, isDev)) {
307307
addFilesToExistingEntryPoint(newEntryProperty, entryPointName, filesToInject);
308308
} else {
309309
if (
@@ -429,14 +429,25 @@ function shouldAddSentryToEntryPoint(
429429
entryPointName: string,
430430
isServer: boolean,
431431
excludeServerRoutes: Array<string | RegExp> = [],
432+
isDev: boolean,
432433
): boolean {
433434
// On the server side, by default we inject the `Sentry.init()` code into every page (with a few exceptions).
434435
if (isServer) {
435436
const entryPointRoute = entryPointName.replace(/^pages/, '');
437+
438+
// User-specified pages to skip. (Note: For ease of use, `excludeServerRoutes` is specified in terms of routes,
439+
// which don't have the `pages` prefix.)
440+
if (stringMatchesSomePattern(entryPointRoute, excludeServerRoutes, true)) {
441+
return false;
442+
}
443+
444+
// In dev mode, page routes aren't considered entrypoints so we inject the init call in the `/_app` entrypoint which
445+
// always exists, even if the user didn't add a `_app` page themselves
446+
if (isDev) {
447+
return entryPointRoute === '/_app';
448+
}
449+
436450
if (
437-
// User-specified pages to skip. (Note: For ease of use, `excludeServerRoutes` is specified in terms of routes,
438-
// which don't have the `pages` prefix.)
439-
stringMatchesSomePattern(entryPointRoute, excludeServerRoutes, true) ||
440451
// All non-API pages contain both of these components, and we don't want to inject more than once, so as long as
441452
// we're doing the individual pages, it's fine to skip these. (Note: Even if a given user doesn't have either or
442453
// both of these in their `pages/` folder, they'll exist as entrypoints because nextjs will supply default
@@ -516,7 +527,7 @@ export function getWebpackPluginOptions(
516527
stripPrefix: ['webpack://_N_E/'],
517528
urlPrefix,
518529
entries: (entryPointName: string) =>
519-
shouldAddSentryToEntryPoint(entryPointName, isServer, userSentryOptions.excludeServerRoutes),
530+
shouldAddSentryToEntryPoint(entryPointName, isServer, userSentryOptions.excludeServerRoutes, isDev),
520531
release: getSentryRelease(buildId),
521532
dryRun: isDev,
522533
});

0 commit comments

Comments
 (0)