Skip to content

Commit

Permalink
fix(nextjs): Include nextjs config's basePath on urlPrefix (#3922)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jemmyphan committed Sep 2, 2021
1 parent 45508c0 commit e71454e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/nextjs/src/config/webpack.ts
Expand Up @@ -247,14 +247,15 @@ function getWebpackPluginOptions(
const isWebpack5 = webpack.version.startsWith('5');
const isServerless = nextConfig.target === 'experimental-serverless-trace';
const hasSentryProperties = fs.existsSync(path.resolve(projectDir, 'sentry.properties'));
const urlPrefix = nextConfig.basePath ? `~${nextConfig.basePath}/_next` : '~/_next';

const serverInclude = isServerless
? [{ paths: ['.next/serverless/'], urlPrefix: '~/_next/serverless' }]
: [{ paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' }].concat(
isWebpack5 ? [{ paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' }] : [],
? [{ paths: ['.next/serverless/'], urlPrefix: `${urlPrefix}/serverless` }]
: [{ paths: ['.next/server/pages/'], urlPrefix: `${urlPrefix}/server/pages` }].concat(
isWebpack5 ? [{ paths: ['.next/server/chunks/'], urlPrefix: `${urlPrefix}/server/chunks` }] : [],
);

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

const defaultPluginOptions = dropUndefinedKeys({
include: isServer ? serverInclude : clientInclude,
Expand All @@ -265,7 +266,7 @@ function getWebpackPluginOptions(
authToken: process.env.SENTRY_AUTH_TOKEN,
configFile: hasSentryProperties ? 'sentry.properties' : undefined,
stripPrefix: ['webpack://_N_E/'],
urlPrefix: `~/_next`,
urlPrefix,
entries: shouldAddSentryToEntryPoint,
release: getSentryRelease(buildId),
dryRun: isDev,
Expand Down
70 changes: 70 additions & 0 deletions packages/nextjs/test/config.test.ts
Expand Up @@ -426,6 +426,76 @@ describe('Sentry webpack plugin config', () => {
});
});

describe("Sentry webpack plugin `include` option with basePath filled on next's config", () => {
const userNextConfigWithBasePath = {
...userNextConfig,
basePath: '/city-park',
};

it('has the correct value when building client bundles', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig: userNextConfigWithBasePath,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: getBuildContext('client', userNextConfigWithBasePath),
});

const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;

expect(sentryWebpackPlugin.options?.include).toEqual([
{ paths: ['.next/static/chunks/pages'], urlPrefix: '~/city-park/_next/static/chunks/pages' },
]);
});

it('has the correct value when building serverless server bundles', async () => {
const userNextConfigServerless = { ...userNextConfigWithBasePath };
userNextConfigServerless.target = 'experimental-serverless-trace';

const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig: userNextConfigServerless,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: getBuildContext('server', userNextConfigServerless),
});

const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;

expect(sentryWebpackPlugin.options?.include).toEqual([
{ paths: ['.next/serverless/'], urlPrefix: '~/city-park/_next/serverless' },
]);
});

it('has the correct value when building serverful server bundles using webpack 4', async () => {
const serverBuildContextWebpack4 = getBuildContext('server', userNextConfigWithBasePath);
serverBuildContextWebpack4.webpack.version = '4.15.13';

const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig: userNextConfigWithBasePath,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContextWebpack4,
});

const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;

expect(sentryWebpackPlugin.options?.include).toEqual([
{ paths: ['.next/server/pages/'], urlPrefix: '~/city-park/_next/server/pages' },
]);
});

it('has the correct value when building serverful server bundles using webpack 5', async () => {
const finalWebpackConfig = await materializeFinalWebpackConfig({
userNextConfig: userNextConfigWithBasePath,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: getBuildContext('server', userNextConfigWithBasePath),
});

const sentryWebpackPlugin = finalWebpackConfig.plugins?.[0] as SentryWebpackPluginType;

expect(sentryWebpackPlugin.options?.include).toEqual([
{ paths: ['.next/server/pages/'], urlPrefix: '~/city-park/_next/server/pages' },
{ paths: ['.next/server/chunks/'], urlPrefix: '~/city-park/_next/server/chunks' },
]);
});
});

it('allows SentryWebpackPlugin to be turned off for client code (independent of server code)', () => {
const clientFinalNextConfig = materializeFinalNextConfig({
...userNextConfig,
Expand Down

0 comments on commit e71454e

Please sign in to comment.