Skip to content

Commit 857c80e

Browse files
committed
prevent Sentry.init() code from being injected into excluded routes
1 parent ed13791 commit 857c80e

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

packages/nextjs/src/config/webpack.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable max-lines */
22
import { getSentryRelease } from '@sentry/node';
3-
import { arrayify, dropUndefinedKeys, escapeStringForRegex, logger } from '@sentry/utils';
3+
import { arrayify, dropUndefinedKeys, escapeStringForRegex, logger, stringMatchesSomePattern } from '@sentry/utils';
44
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
55
import * as chalk from 'chalk';
66
import * as fs from 'fs';
@@ -139,7 +139,7 @@ export function constructWebpackConfigFunction(
139139
// will call the callback which will call `f` which will call `x.y`... and on and on. Theoretically this could also
140140
// be fixed by using `bind`, but this is way simpler.)
141141
const origEntryProperty = newConfig.entry;
142-
newConfig.entry = async () => addSentryToEntryProperty(origEntryProperty, buildContext);
142+
newConfig.entry = async () => addSentryToEntryProperty(origEntryProperty, buildContext, userSentryOptions);
143143

144144
// Enable the Sentry plugin (which uploads source maps to Sentry when not in dev) by default
145145
if (shouldEnableWebpackPlugin(buildContext, userSentryOptions)) {
@@ -252,6 +252,7 @@ function findTranspilationRules(rules: WebpackModuleRule[] | undefined, projectD
252252
async function addSentryToEntryProperty(
253253
currentEntryProperty: WebpackEntryProperty,
254254
buildContext: BuildContext,
255+
userSentryOptions: UserSentryOptions,
255256
): Promise<EntryPropertyObject> {
256257
// The `entry` entry in a webpack config can be a string, array of strings, object, or function. By default, nextjs
257258
// sets it to an async function which returns the promise of an object of string arrays. Because we don't know whether
@@ -272,7 +273,7 @@ async function addSentryToEntryProperty(
272273

273274
// inject into all entry points which might contain user's code
274275
for (const entryPointName in newEntryProperty) {
275-
if (shouldAddSentryToEntryPoint(entryPointName, isServer)) {
276+
if (shouldAddSentryToEntryPoint(entryPointName, isServer, userSentryOptions.excludeServerRoutes)) {
276277
addFilesToExistingEntryPoint(newEntryProperty, entryPointName, filesToInject);
277278
}
278279
}
@@ -381,13 +382,21 @@ function checkWebpackPluginOverrides(
381382
*
382383
* @param entryPointName The name of the entry point in question
383384
* @param isServer Whether or not this function is being called in the context of a server build
385+
* @param excludeServerRoutes A list of excluded serverside entrypoints provided by the user
384386
* @returns `true` if sentry code should be injected, and `false` otherwise
385387
*/
386-
function shouldAddSentryToEntryPoint(entryPointName: string, isServer: boolean): boolean {
388+
function shouldAddSentryToEntryPoint(
389+
entryPointName: string,
390+
isServer: boolean,
391+
excludeServerRoutes: Array<string | RegExp> = [],
392+
): boolean {
387393
// On the server side, by default we inject the `Sentry.init()` code into every page (with a few exceptions).
388394
if (isServer) {
389395
const entryPointRoute = entryPointName.replace(/^pages/, '');
390396
if (
397+
// User-specified pages to skip. (Note: For ease of use, `excludeServerRoutes` is specified in terms of routes,
398+
// which don't have the `pages` prefix.)
399+
stringMatchesSomePattern(entryPointRoute, excludeServerRoutes, true) ||
391400
// All non-API pages contain both of these components, and we don't want to inject more than once, so as long as
392401
// we're doing the individual pages, it's fine to skip these. (Note: Even if a given user doesn't have either or
393402
// both of these in their `pages/` folder, they'll exist as entrypoints because nextjs will supply default

0 commit comments

Comments
 (0)