Skip to content

Commit e71454e

Browse files
authored
fix(nextjs): Include nextjs config's basePath on urlPrefix (#3922)
When using `SentryWebpackPlugin` as part of the nextjs SDK, uploaded sourcemap names point to the wrong path if the nextjs `basePath` setting is used. This makes it so that the `urlPrefix` value includes `basePath`, if set.
1 parent 45508c0 commit e71454e

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,15 @@ function getWebpackPluginOptions(
247247
const isWebpack5 = webpack.version.startsWith('5');
248248
const isServerless = nextConfig.target === 'experimental-serverless-trace';
249249
const hasSentryProperties = fs.existsSync(path.resolve(projectDir, 'sentry.properties'));
250+
const urlPrefix = nextConfig.basePath ? `~${nextConfig.basePath}/_next` : '~/_next';
250251

251252
const serverInclude = isServerless
252-
? [{ paths: ['.next/serverless/'], urlPrefix: '~/_next/serverless' }]
253-
: [{ paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' }].concat(
254-
isWebpack5 ? [{ paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' }] : [],
253+
? [{ paths: ['.next/serverless/'], urlPrefix: `${urlPrefix}/serverless` }]
254+
: [{ paths: ['.next/server/pages/'], urlPrefix: `${urlPrefix}/server/pages` }].concat(
255+
isWebpack5 ? [{ paths: ['.next/server/chunks/'], urlPrefix: `${urlPrefix}/server/chunks` }] : [],
255256
);
256257

257-
const clientInclude = [{ paths: ['.next/static/chunks/pages'], urlPrefix: '~/_next/static/chunks/pages' }];
258+
const clientInclude = [{ paths: ['.next/static/chunks/pages'], urlPrefix: `${urlPrefix}/static/chunks/pages` }];
258259

259260
const defaultPluginOptions = dropUndefinedKeys({
260261
include: isServer ? serverInclude : clientInclude,
@@ -265,7 +266,7 @@ function getWebpackPluginOptions(
265266
authToken: process.env.SENTRY_AUTH_TOKEN,
266267
configFile: hasSentryProperties ? 'sentry.properties' : undefined,
267268
stripPrefix: ['webpack://_N_E/'],
268-
urlPrefix: `~/_next`,
269+
urlPrefix,
269270
entries: shouldAddSentryToEntryPoint,
270271
release: getSentryRelease(buildId),
271272
dryRun: isDev,

packages/nextjs/test/config.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,76 @@ describe('Sentry webpack plugin config', () => {
426426
});
427427
});
428428

429+
describe("Sentry webpack plugin `include` option with basePath filled on next's config", () => {
430+
const userNextConfigWithBasePath = {
431+
...userNextConfig,
432+
basePath: '/city-park',
433+
};
434+
435+
it('has the correct value when building client bundles', async () => {
436+
const finalWebpackConfig = await materializeFinalWebpackConfig({
437+
userNextConfig: userNextConfigWithBasePath,
438+
incomingWebpackConfig: clientWebpackConfig,
439+
incomingWebpackBuildContext: getBuildContext('client', userNextConfigWithBasePath),
440+
});
441+
442+
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
443+
444+
expect(sentryWebpackPlugin.options?.include).toEqual([
445+
{ paths: ['.next/static/chunks/pages'], urlPrefix: '~/city-park/_next/static/chunks/pages' },
446+
]);
447+
});
448+
449+
it('has the correct value when building serverless server bundles', async () => {
450+
const userNextConfigServerless = { ...userNextConfigWithBasePath };
451+
userNextConfigServerless.target = 'experimental-serverless-trace';
452+
453+
const finalWebpackConfig = await materializeFinalWebpackConfig({
454+
userNextConfig: userNextConfigServerless,
455+
incomingWebpackConfig: serverWebpackConfig,
456+
incomingWebpackBuildContext: getBuildContext('server', userNextConfigServerless),
457+
});
458+
459+
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
460+
461+
expect(sentryWebpackPlugin.options?.include).toEqual([
462+
{ paths: ['.next/serverless/'], urlPrefix: '~/city-park/_next/serverless' },
463+
]);
464+
});
465+
466+
it('has the correct value when building serverful server bundles using webpack 4', async () => {
467+
const serverBuildContextWebpack4 = getBuildContext('server', userNextConfigWithBasePath);
468+
serverBuildContextWebpack4.webpack.version = '4.15.13';
469+
470+
const finalWebpackConfig = await materializeFinalWebpackConfig({
471+
userNextConfig: userNextConfigWithBasePath,
472+
incomingWebpackConfig: serverWebpackConfig,
473+
incomingWebpackBuildContext: serverBuildContextWebpack4,
474+
});
475+
476+
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
477+
478+
expect(sentryWebpackPlugin.options?.include).toEqual([
479+
{ paths: ['.next/server/pages/'], urlPrefix: '~/city-park/_next/server/pages' },
480+
]);
481+
});
482+
483+
it('has the correct value when building serverful server bundles using webpack 5', async () => {
484+
const finalWebpackConfig = await materializeFinalWebpackConfig({
485+
userNextConfig: userNextConfigWithBasePath,
486+
incomingWebpackConfig: serverWebpackConfig,
487+
incomingWebpackBuildContext: getBuildContext('server', userNextConfigWithBasePath),
488+
});
489+
490+
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
491+
492+
expect(sentryWebpackPlugin.options?.include).toEqual([
493+
{ paths: ['.next/server/pages/'], urlPrefix: '~/city-park/_next/server/pages' },
494+
{ paths: ['.next/server/chunks/'], urlPrefix: '~/city-park/_next/server/chunks' },
495+
]);
496+
});
497+
});
498+
429499
it('allows SentryWebpackPlugin to be turned off for client code (independent of server code)', () => {
430500
const clientFinalNextConfig = materializeFinalNextConfig({
431501
...userNextConfig,

0 commit comments

Comments
 (0)