Skip to content

Commit a172eb2

Browse files
committed
fix(nextjs): Include nextjs config's basePath on urlPrefix
1 parent 36ee3d6 commit a172eb2

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
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: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,17 @@ async function materializeFinalWebpackConfig(options: {
157157
userSentryWebpackPluginConfig,
158158
);
159159

160+
// update incomingWebpackBuildContext's config
161+
const updatedIncomingWebpackBuildContext = {
162+
...incomingWebpackBuildContext,
163+
config: {
164+
...incomingWebpackBuildContext.config,
165+
...userNextConfig,
166+
},
167+
};
168+
160169
// call it to get concrete values for comparison
161-
const finalWebpackConfigValue = webpackConfigFunction(incomingWebpackConfig, incomingWebpackBuildContext);
170+
const finalWebpackConfigValue = webpackConfigFunction(incomingWebpackConfig, updatedIncomingWebpackBuildContext);
162171
const webpackEntryProperty = finalWebpackConfigValue.entry as EntryPropertyFunction;
163172
finalWebpackConfigValue.entry = await webpackEntryProperty();
164173

@@ -420,6 +429,70 @@ describe('Sentry webpack plugin config', () => {
420429
});
421430
});
422431

432+
describe("Sentry webpack plugin `include` option with basePath filled on next's config", () => {
433+
const withBaseUrlNextConfig = {
434+
...userNextConfig,
435+
basePath: '/city-park',
436+
};
437+
438+
it('has the correct value when building client bundles', async () => {
439+
const finalWebpackConfig = await materializeFinalWebpackConfig({
440+
userNextConfig: withBaseUrlNextConfig,
441+
incomingWebpackConfig: clientWebpackConfig,
442+
incomingWebpackBuildContext: clientBuildContext,
443+
});
444+
445+
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
446+
447+
expect(sentryWebpackPlugin.options?.include).toEqual([
448+
{ paths: ['.next/static/chunks/pages'], urlPrefix: '~/city-park/_next/static/chunks/pages' },
449+
]);
450+
});
451+
452+
it('has the correct value when building serverless server bundles', async () => {
453+
const finalWebpackConfig = await materializeFinalWebpackConfig({
454+
userNextConfig: withBaseUrlNextConfig,
455+
incomingWebpackConfig: serverWebpackConfig,
456+
incomingWebpackBuildContext: { ...serverBuildContext, config: { target: 'experimental-serverless-trace' } },
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 finalWebpackConfig = await materializeFinalWebpackConfig({
468+
userNextConfig: withBaseUrlNextConfig,
469+
incomingWebpackConfig: serverWebpackConfig,
470+
incomingWebpackBuildContext: { ...serverBuildContext, webpack: { version: '4.15.13' } },
471+
});
472+
473+
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
474+
475+
expect(sentryWebpackPlugin.options?.include).toEqual([
476+
{ paths: ['.next/server/pages/'], urlPrefix: '~/city-park/_next/server/pages' },
477+
]);
478+
});
479+
480+
it('has the correct value when building serverful server bundles using webpack 5', async () => {
481+
const finalWebpackConfig = await materializeFinalWebpackConfig({
482+
userNextConfig: withBaseUrlNextConfig,
483+
incomingWebpackConfig: serverWebpackConfig,
484+
incomingWebpackBuildContext: serverBuildContext,
485+
});
486+
487+
const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;
488+
489+
expect(sentryWebpackPlugin.options?.include).toEqual([
490+
{ paths: ['.next/server/pages/'], urlPrefix: '~/city-park/_next/server/pages' },
491+
{ paths: ['.next/server/chunks/'], urlPrefix: '~/city-park/_next/server/chunks' },
492+
]);
493+
});
494+
});
495+
423496
it('allows SentryWebpackPlugin to be turned off for client code (independent of server code)', () => {
424497
const clientFinalNextConfig = materializeFinalNextConfig({
425498
...userNextConfig,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/types/global" />
33
/// <reference types="next/image-types/global" />
4+
5+
// NOTE: This file should not be edited
6+
// see https://nextjs.org/docs/basic-features/typescript for more information.

0 commit comments

Comments
 (0)