Created
October 21, 2019 14:27
-
-
Save merlox/82bfd2605aa4098e4f9f1dc16a6b363a to your computer and use it in GitHub Desktop.
Here's how you enable brotli to load your web apps 95.5% faster (or 50% faster if you're using webpack -p)
This file contains 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
// Webpack.config.js | |
const brotliPlugin = require('brotli-gzip-webpack-plugin') | |
// Inside your config | |
plugins: [ | |
new brotliPlugin({ | |
asset: '[file].br[query]', | |
algorithm: 'brotli', | |
test: /\.(js|css|html|svg)$/, | |
threshold: 10240, | |
minRatio: 0.8, | |
quality: NODE_ENV != 'production' ? 0 : 11, | |
}), | |
new brotliPlugin({ | |
asset: '[file].gz[query]', | |
algorithm: 'gzip', | |
test: /\.(js|css|html|svg)$/, | |
threshold: 10240, | |
minRatio: 0.8, | |
quality: NODE_ENV != 'production' ? 0 : 11, | |
}), | |
] | |
// Server.js | |
if (req.header('Accept-Encoding').includes('br')) { | |
res.set('Content-Encoding', 'br') | |
res.set('Content-Type', 'application/javascript; charset=UTF-8') | |
return res.sendFile(join(__dirname, '..', 'dist', 'build.js.br')) | |
} else if(req.header('Accept-Encoding').includes('gz')) { | |
res.set('Content-Encoding', 'gzip') | |
res.set('Content-Type', 'application/javascript; charset=UTF-8') | |
return res.sendFile(join(__dirname, '..', 'dist', 'build.js.gz')) | |
} |
If you want to learn web development, check my new course heatoon.com which I made by myself to teach you everything you need to know to start working as a fullstack web developer in 2 weeks. Available now on heatoon.com
Awesome stuff!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here are the results of the experiment for a new project, you may get way better results based on your project size:
Webpack -dev without compression
3.6 MB
20 s
Webpack -production without compression
215 KB
1.73 s
Webpack -production with compression
60.1 KB
889 ms -> 95.5% faster than dev uncompressed, 52% faster than dev production
Gzip
68.9 KB
946 ms