Skip to content

Instantly share code, notes, and snippets.

@merlox
Created October 21, 2019 14:27
Show Gist options
  • Save merlox/82bfd2605aa4098e4f9f1dc16a6b363a to your computer and use it in GitHub Desktop.
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)
// 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'))
}
@merlox
Copy link
Author

merlox commented Oct 21, 2019

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

@hyfydistro
Copy link

Awesome stuff!

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