Supposedly the compression-webpack-plugin is supposed to do it.
I installed the plugin with npm
npm install compression-webpack-plugin --save-dev
And edited my webpack.config.js file to include
const CompressionPlugin = require('compression-webpack-plugin');
...
plugins: [
new CompressionPlugin({
filename: "[path].gz[query]",
algorithm: "gzip",
test: /.(js|css)$/i,
}),
...
When I used page insights to check how fast my webpage loads, it looks like my gz files aren’t recognized, or at least one of them isn’t recognized
This is my main directory
This question is pretty similar to my question. I am trying to avoid using .htaccess though because I heard somewhere that it’s not the best to use with react and webpack. Perhaps this is wrong?
I tried using kushalvm’s solution, but it is not working for me.
3
Answers
Short answer: the kushalvm’s solution is not complete. In order to compress the page size with gzip/brotli, there are two steps:
.gz
/.br
files in build time (or dynamically generate them by a server).js
files)And you are doing the first part but not the second one. Because when you build a react project (in case of client-side rendering – CSR) you simply create one
.html
file importing some script tags (your react project). If you use brotli or compression plugin for webpack you have created some.gz/.br
files, but still, the HTML file imports the old.js
scripts.So what are the different methods you can use to serve compressed files you need to configure your server (you cannot do this just by changing your React configs)
Solutions
1)If you are using client-side rendering, you can create a custom express server used to serve files, it is very simple and takes less than 10 lines of code, you can read the doc about this in the Official React Docs (with
express-static-gzip
)So your server file will look like this:
This server, will read the files in the build directory, and if a request for some js assets arrives, it checks (if the browser supports the compression, and if compression file exists for it) then serves compressed files
2) If you use SSR (for example
Next.js
orReact-Starter-kit
) you can create a custom express server, and use the same approach above.3) If you use Apache webserver, you can use Apache gzip/brotli compressions config file like this:
4) If you use a third party JamStack/CDN provider like Netlify or AWS, they have some configs for you to enable dynamic gzip.
You can also Build Time gzip. here is the process.
Install the Webpack compression plugin
npm install compression-webpack-plugin –save-dev
Install and Import the plugin var CompressionPlugin = require(‘compression-webpack-plugin’);
Add it to plugins array
I was also struggling with serving the compressed version with my React SSR app; in my case the brotli type with js.br extension > "bundle.js.br". The following snippet helped me in the end to serve it properly. I reduced my client-side uncompressed bundle in development mode from 1.7 MB to 0.27 MB. Try and figure it out for you. There is no extra plugin needed to serve the file. Of course, the compressed version must be generated first with webpack. For webpack brotli compression I use the npmjs packages compression-webpack-plugin in conjunction with zlib.
Now let me explain my snippet above which I use for my SSR React app to serve the compressed file.