Skip to content

fix(nextjs): handle assetPrefix #6241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/nextjs/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export type NextConfigObject = {
distDir?: string;
// The root at which the nextjs app will be served (defaults to "/")
basePath?: string;
// The asset prefix (pathname or full URL) if assets will not be stored at the default Next.js location
assetPrefix?: string;
// Config which will be available at runtime
publicRuntimeConfig?: { [key: string]: unknown };
// File extensions that count as pages in the `pages/` directory
Expand Down
29 changes: 22 additions & 7 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
import * as chalk from 'chalk';
import * as fs from 'fs';
import * as path from 'path';
import * as url from 'url';

import {
BuildContext,
Expand Down Expand Up @@ -457,17 +458,32 @@ export function getWebpackPluginOptions(
const isWebpack5 = webpack.version.startsWith('5');
const isServerless = userNextConfig.target === 'experimental-serverless-trace';
const hasSentryProperties = fs.existsSync(path.resolve(projectDir, 'sentry.properties'));
const urlPrefix = userNextConfig.basePath ? `~${userNextConfig.basePath}/_next` : '~/_next';

let basePath = userNextConfig.basePath || '';
if (basePath) {
basePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;
}
const serverUrlPrefix = `~${basePath}/_next`;

let assetPrefix = userNextConfig.assetPrefix || userNextConfig.basePath || '';
if (assetPrefix) {
const assertPrefixUrl = url.parse(assetPrefix);
assetPrefix = assertPrefixUrl.pathname || '';
assetPrefix = assetPrefix.endsWith('/') ? assetPrefix.slice(0, -1) : assetPrefix;
}
const clientUrlPrefix = `~${assetPrefix}/_next`;

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

const clientInclude = userSentryOptions.widenClientFileUpload
? [{ paths: [`${distDirAbsPath}/static/chunks`], urlPrefix: `${urlPrefix}/static/chunks` }]
: [{ paths: [`${distDirAbsPath}/static/chunks/pages`], urlPrefix: `${urlPrefix}/static/chunks/pages` }];
? [{ paths: [`${distDirAbsPath}/static/chunks`], urlPrefix: `${clientUrlPrefix}/static/chunks` }]
: [{ paths: [`${distDirAbsPath}/static/chunks/pages`], urlPrefix: `${clientUrlPrefix}/static/chunks/pages` }];

const defaultPluginOptions = dropUndefinedKeys({
include: isServer ? serverInclude : clientInclude,
Expand All @@ -484,7 +500,6 @@ export function getWebpackPluginOptions(
authToken: process.env.SENTRY_AUTH_TOKEN,
configFile: hasSentryProperties ? 'sentry.properties' : undefined,
stripPrefix: ['webpack://_N_E/'],
urlPrefix,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed this because:

(a) urlPrefix is specified via include and so it has no effect
(b) the correct value depends on whether it's a server or client build

I could update it to isServer ? serverUrlPrefix : clientUrlPrefix if we still want this.

entries: (entryPointName: string) =>
shouldAddSentryToEntryPoint(entryPointName, isServer, userSentryOptions.excludeServerRoutes),
release: getSentryRelease(buildId),
Expand Down
156 changes: 155 additions & 1 deletion packages/nextjs/test/config/webpack/sentryWebpackPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ describe('Sentry webpack plugin config', () => {
project: 'simulator', // from user webpack plugin config
authToken: 'dogsarebadatkeepingsecrets', // picked up from env
stripPrefix: ['webpack://_N_E/'], // default
urlPrefix: '~/_next', // default
entries: expect.any(Function), // default, tested separately elsewhere
release: 'doGsaREgReaT', // picked up from env
dryRun: false, // based on buildContext.dev being false
Expand Down Expand Up @@ -295,6 +294,161 @@ describe('Sentry webpack plugin config', () => {
});
});

describe('Sentry webpack plugin `includes` option with assetPrefix set', () => {
it('does not affect server build', async () => {
const exportedNextConfigWithAssetPrefix = {
...exportedNextConfig,
assetPrefix: '/asset-prefix',
};
const serverBuildContextWebpack4 = getBuildContext('server', exportedNextConfigWithAssetPrefix);
serverBuildContextWebpack4.webpack.version = '4.15.13';

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

const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{
paths: [`${serverBuildContextWebpack4.dir}/.next/server/pages/`],
urlPrefix: '~/_next/server/pages',
},
]);
});

it('has the correct value given a path', async () => {
const exportedNextConfigWithAssetPrefix = {
...exportedNextConfig,
assetPrefix: '/asset-prefix',
};
const buildContext = getBuildContext('client', exportedNextConfigWithAssetPrefix);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithAssetPrefix,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: buildContext,
});

const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{
paths: [`${buildContext.dir}/.next/static/chunks/pages`],
urlPrefix: '~/asset-prefix/_next/static/chunks/pages',
},
]);
});

it('has the correct value given a path with a leading slash', async () => {
const exportedNextConfigWithAssetPrefix = {
...exportedNextConfig,
assetPrefix: '/asset-prefix/',
};
const buildContext = getBuildContext('client', exportedNextConfigWithAssetPrefix);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithAssetPrefix,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: buildContext,
});

const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{
paths: [`${buildContext.dir}/.next/static/chunks/pages`],
urlPrefix: '~/asset-prefix/_next/static/chunks/pages',
},
]);
});

it('has the correct value when given a full URL with no path', async () => {
const exportedNextConfigWithAssetPrefix = {
...exportedNextConfig,
assetPrefix: 'https://cdn.mydomain.com',
};
const buildContext = getBuildContext('client', exportedNextConfigWithAssetPrefix);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithAssetPrefix,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: buildContext,
});

const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{
paths: [`${buildContext.dir}/.next/static/chunks/pages`],
urlPrefix: '~/_next/static/chunks/pages',
},
]);
});

it('has the correct value when given a full URL with a path', async () => {
const exportedNextConfigWithAssetPrefix = {
...exportedNextConfig,
assetPrefix: 'https://cdn.mydomain.com/asset-prefix',
};
const buildContext = getBuildContext('client', exportedNextConfigWithAssetPrefix);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithAssetPrefix,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: buildContext,
});

const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{
paths: [`${buildContext.dir}/.next/static/chunks/pages`],
urlPrefix: '~/asset-prefix/_next/static/chunks/pages',
},
]);
});

it('takes priority over basePath ', async () => {
const exportedNextConfigWithAssetPrefix = {
...exportedNextConfig,
assetPrefix: '/asset-prefix',
basePath: '/base-path',
};
const buildContext = getBuildContext('client', exportedNextConfigWithAssetPrefix);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithAssetPrefix,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: buildContext,
});

const sentryWebpackPluginInstance = findWebpackPlugin(
finalWebpackConfig,
'SentryCliPlugin',
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{
paths: [`${buildContext.dir}/.next/static/chunks/pages`],
urlPrefix: '~/asset-prefix/_next/static/chunks/pages',
},
]);
});
});

describe('SentryWebpackPlugin enablement', () => {
let processEnvBackup: typeof process.env;

Expand Down