Skip to content
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

@sentry/nextjs: option for hidden-source-map instead of source-map #3549

Closed
Pikachews opened this issue May 17, 2021 · 11 comments · Fixed by #4436
Closed

@sentry/nextjs: option for hidden-source-map instead of source-map #3549

Pikachews opened this issue May 17, 2021 · 11 comments · Fixed by #4436

Comments

@Pikachews
Copy link

Pikachews commented May 17, 2021

Firstly, thanks for all your hard work, Sentry is one of the best development tools out there, hands down. 👍

I have a small request for the new @sentry/nextjs plugin.
Based on this line here:

newConfig.devtool = 'source-map';

It seems the plugin only allows using source-map for devtools. Would it be possible to add an option to use hidden-source-map instead? The current configuration leaves the reference to the source map file, causing Chrome DevTools to report a bunch of

DevTools failed to load SourceMap: Could not load content for https://...: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE

since I do not upload the source maps to the server. 🙁

Thanks for the consideration.

@Ryz0nd
Copy link

Ryz0nd commented May 17, 2021

I temporarily solved this problem using patch-package.

@kamilogorek
Copy link
Contributor

withSentryConfig which is used to generate the config is nothing else than a function that processes input config, and outputs updated one. There's nothing that'd stop you from changing any of the values once it's processed.

Based on this docs: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#extend-default-webpack-usage-for-source-maps

Given this config (shortened for readabilty):

const { withSentryConfig } = require("@sentry/nextjs");
// ...
module.exports = withSentryConfig(moduleExports, SentryWebpackPluginOptions);

You can write:

const { withSentryConfig } = require("@sentry/nextjs");
// ...
const sentryConfig = withSentryConfig(moduleExports, SentryWebpackPluginOptions);
const stashedWebpackConfig = sentryConfig.webpack;
sentryConfig.webpack = (config, options) => {
  const updatedConfig = stashedWebpackConfig(config, options);
  // see: https://github.com/vercel/next.js/blob/master/errors/improper-devtool.md
  if (!options.dev) {
    updatedConfig.devtool = 'hidden-source-map';
  }
  return updatedConfig;
}
module.exports = sentryConfig;

While definitely not perfect, it should let you get unblocked.

@lobsterkatie
Copy link
Member

We are considering whether (and if so, when and how best) to do this. Reopening until we come to a decision.

@lobsterkatie lobsterkatie reopened this Jul 22, 2021
@jgibbons
Copy link

I tried the solution referenced above to set hidden-source-map but it resulted in Sentry no longer using source maps for SSR pages. Is this a known issue or perhaps something simple I'm missing?

@hellojser
Copy link

class HiddenSourceMapPlugin {
  apply(compiler) {
    compiler.hooks.environment.tap('HiddenSourceMapPlugin', () => {
      compiler.options.devtool =
        compiler.options.name === 'server' ? false : 'hidden-source-map';
    });
  }
}

@AlexBeauchemin
Copy link

I created a webpack plugin which remove all sourcemaps after sentry's upload and implement @hellojser solution to remove the sourcemap references to avoid 404 errors by browser calling the unexisting map files

https://github.com/AlexBeauchemin/webpack-delete-sourcemaps-plugin

lobsterkatie added a commit that referenced this issue Jan 24, 2022
…ol` value (#4436)

We currently force the value of the webpack option `devtool` to be `source-map`, in order to guarantee that the correct sourcemaps are generated during build. There is another `devtool` value[1], `hidden-source-map`, which produces the same maps (so just as good for our purposes), but without adding `sourceMappingURL` comments at the bottom of the resulting JS files. (Some users prefer this as a way not to have the browser complain when it tries to follow the `sourceMappingURL` link and comes up empty.)

This PR adds an option that users can pass in their nextjs config, under the key `sentry.hideSourceMaps`. When this is set to true, the SDK will use `hidden-source-map` as the `devtool` value instead of `source-map`. Note that since this is a front-end-only problem, the option only applies to client-side builds.

The new option is documented in getsentry/sentry-docs#4627.

Fixes #3549.

[1] https://webpack.js.org/configuration/devtool/
@disguised-uchiha
Copy link

disguised-uchiha commented Jul 25, 2022

class HiddenSourceMapPlugin {
  apply(compiler) {
    compiler.hooks.environment.tap('HiddenSourceMapPlugin', () => {
      compiler.options.devtool =
        compiler.options.name === 'server' ? false : 'hidden-source-map';
    });
  }
}

This worked for me. No need to install another plugin.
I'm using nextjs.

All I did was paste the following code in my next.config.js file:

class HiddenSourceMapPlugin {
   apply(compiler) {
     compiler.hooks.environment.tap('HiddenSourceMapPlugin', () => {
       compiler.options.devtool =
         compiler.options.name === 'server' ? false : 'hidden-source-map';
     });
   }
 }

 const nextConfig = {
  webpack(config) {
    config.plugins.push(new HiddenSourceMapPlugin());
    return config;
  },
};

@lobsterkatie
Copy link
Member

@disguised-uchiha You don't need to even go through all of that. Check out the change in #4436.

@mAAdhaTTah
Copy link

Hey @lobsterkatie thanks for enabling this! QQ: why does this require the Webpack plugin to be enabled to work? We run our builds in Docker, so we can't use the Sentry Webpack plugin to upload at the same time (the asset uploading is a separate step after the build has completed). We're enabling hidden-source-maps ourselves so it's not a huge deal, but would be nice to have one bit less of configuration.

@lobsterkatie
Copy link
Member

QQ: why does this require the Webpack plugin to be enabled to work?

It's a good question. You're right that sourcemap creation and sourcemap uploading aren't necessarily tied to one another - there's no technical reason for the requirement. The reason it exists, though, is because we don't want to be changing users' webpack configs any more than we have to. If the sentry webpack plugin is disabled, the reason for us to set the devtool value also disappears, and so we leave it alone.

@mAAdhaTTah
Copy link

@lobsterkatie Fair enough, thanks for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet