Skip to content

Commit 27b5771

Browse files
authored
ref(nextjs): Add warning about non-hidden sourcemaps (#5649)
This adds a warning during nextjs app build (both in prod and dev) letting folks know that by default (for now), the sourcemaps we create can be seen by browser devtools. In conjunction with getsentry/sentry-wizard#188, getsentry/sentry-docs#5464, and vercel/next.js#40079, this is the first step in addressing the concerns raised in #4489. See #4489 (comment) for more details. Notes: - The function doing the warning includes both the current warning and one for use in v8 and beyond (currently commented out), telling people they no longer need to set `hideSourceMaps` to `true` (because by that point it will be the default). - The formatting of the warning matches the formatting of other nextjs warnings. Because nextjs vendors `chalk` themselves, that meant we needed to add it as a dependency. Though the latest version of `chalk` is 5.x, here we're using 3.x because it's the last to be compatible with node 8. (See the PR for a screenshot.) - There are `TODO`s reflecting the changes which need to be made in v8 (change the default to `true`, switch the warning message) and v9 or v10 (get rid of the second warning once it's had plenty of time to do its job).
1 parent 7542482 commit 27b5771

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

packages/nextjs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@sentry/types": "7.11.1",
2929
"@sentry/utils": "7.11.1",
3030
"@sentry/webpack-plugin": "1.19.0",
31+
"chalk": "3.0.0",
3132
"jscodeshift": "^0.13.1",
3233
"rollup": "2.78.0",
3334
"tslib": "^1.9.3"

packages/nextjs/src/config/webpack.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { getSentryRelease } from '@sentry/node';
33
import { dropUndefinedKeys, escapeStringForRegex, logger } from '@sentry/utils';
44
import { default as SentryWebpackPlugin } from '@sentry/webpack-plugin';
5+
import * as chalk from 'chalk';
56
import * as fs from 'fs';
67
import * as path from 'path';
78

@@ -138,9 +139,14 @@ export function constructWebpackConfigFunction(
138139
// TODO Handle possibility that user is using `SourceMapDevToolPlugin` (see
139140
// https://webpack.js.org/plugins/source-map-dev-tool-plugin/)
140141

142+
// TODO (v9 or v10, maybe): Remove this
143+
handleSourcemapHidingOptionWarning(userSentryOptions, isServer);
144+
141145
// Next doesn't let you change `devtool` in dev even if you want to, so don't bother trying - see
142146
// https://github.com/vercel/next.js/blob/master/errors/improper-devtool.md
143147
if (!isDev) {
148+
// TODO (v8): Default `hideSourceMaps` to `true`
149+
144150
// `hidden-source-map` produces the same sourcemaps as `source-map`, but doesn't include the `sourceMappingURL`
145151
// comment at the bottom. For folks who aren't publicly hosting their sourcemaps, this is helpful because then
146152
// the browser won't look for them and throw errors into the console when it can't find them. Because this is a
@@ -494,3 +500,51 @@ function shouldEnableWebpackPlugin(buildContext: BuildContext, userSentryOptions
494500
// We've passed all of the tests!
495501
return true;
496502
}
503+
504+
/** Handle warning messages about `hideSourceMaps` option. Can be removed in v9 or v10 (or whenever we consider that
505+
* enough people will have upgraded the SDK that the warning about the default in v8 - currently commented out - is
506+
* overkill). */
507+
function handleSourcemapHidingOptionWarning(userSentryOptions: UserSentryOptions, isServer: boolean): void {
508+
// This is nextjs's own logging formatting, vendored since it's not exported. See
509+
// https://github.com/vercel/next.js/blob/c3ceeb03abb1b262032bd96457e224497d3bbcef/packages/next/build/output/log.ts#L3-L11
510+
// and
511+
// https://github.com/vercel/next.js/blob/de7aa2d6e486c40b8be95a1327639cbed75a8782/packages/next/lib/eslint/runLintCheck.ts#L321-L323.
512+
const codeFormat = (str: string): string => chalk.bold.cyan(str);
513+
514+
const _warningPrefix_ = `${chalk.yellow('warn')} -`;
515+
const _sentryNextjs_ = codeFormat('@sentry/nextjs');
516+
const _hideSourceMaps_ = codeFormat('hideSourceMaps');
517+
const _true_ = codeFormat('true');
518+
const _false_ = codeFormat('false');
519+
const _sentry_ = codeFormat('sentry');
520+
const _nextConfigJS_ = codeFormat('next.config.js');
521+
522+
if (isServer && userSentryOptions.hideSourceMaps === undefined) {
523+
// eslint-disable-next-line no-console
524+
console.warn(
525+
`\n${_warningPrefix_} In order to be able to deminify errors, ${_sentryNextjs_} creates sourcemaps and uploads ` +
526+
'them to the Sentry server. Depending on your deployment setup, this means your original code may be visible ' +
527+
`in browser devtools in production. To prevent this, set ${_hideSourceMaps_} to ${_true_} in the ${_sentry_} ` +
528+
`options in your ${_nextConfigJS_}. To disable this warning without changing sourcemap behavior, set ` +
529+
`${_hideSourceMaps_} to ${_false_}. (In ${_sentryNextjs_} version 8.0.0 and beyond, this option will default ` +
530+
`to ${_true_}.) See https://webpack.js.org/configuration/devtool/ and ` +
531+
'https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#use-hidden-source-map for more ' +
532+
'information.\n',
533+
);
534+
}
535+
536+
// TODO (v8): Remove the check above in favor of the one below
537+
538+
// const infoPrefix = `${chalk.cyan('info')} -`;
539+
//
540+
// if (isServer && userSentryOptions.hideSourceMaps === true) {
541+
// // eslint-disable-next-line no-console
542+
// console.log(
543+
// `\n${infoPrefix} Starting in ${_sentryNextjs_} version 8.0.0, ${_hideSourceMaps_} defaults to ${_true_}, and ` +
544+
// `thus can be removed from the ${_sentry_} options in ${_nextConfigJS_}. See ` +
545+
// 'https://webpack.js.org/configuration/devtool/ and ' +
546+
// 'https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#use-hidden-source-map for more ' +
547+
// 'information.\n',
548+
// );
549+
// }
550+
}

0 commit comments

Comments
 (0)