Last active
September 20, 2023 20:45
-
-
Save kamranayub/1cef1362632f3f259bf0b4874bfa40d8 to your computer and use it in GitHub Desktop.
React Production Profiling Support for Next.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// See: https://kentcdodds.com/blog/profile-a-react-app-for-performance#build-and-measure-the-production-app | |
// See: https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config | |
const TerserPlugin = require('next/dist/compiled/terser-webpack-plugin'); | |
module.exports = { | |
webpack: (config, options) => { | |
// | |
// Use profiler-enabled React builds | |
// | |
config.resolve.alias = { | |
...config.resolve.alias, | |
'react-dom$': 'react-dom/profiling', | |
'scheduler/tracing': 'scheduler/tracing-profiling', | |
}; | |
// | |
// Disable mangling for easier profiling | |
// WARNING: This increases bundle size, DO NOT DO THIS in production! | |
// | |
const terser = config.optimization.minimizer.find((plugin) => plugin instanceof TerserPlugin); | |
if (terser) { | |
terser.options.terserOptions = { | |
...terser.options.terserOptions, | |
keep_classnames: true, | |
keep_fnames: true, | |
}; | |
} | |
return config; | |
} | |
} |
This doesn't seem to work with Next 11. Next appears to handle minification via Terser internally now, rather than adding a plugin to the Webpack config:
I had to patch the minify function directly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you could safely remove L4 and replace L22 with this as well, if you wanted to maybe remove a possible internal dependency:
That will search for the
TerserPlugin
without comparing the exact instance. 🤷 Up to you!Note: I originally tried
options.webpack.TerserPlugin
but that didn't work 😢