Skip to content

Commit c43844b

Browse files
committed
feat: Add brotli pre-compress support
Brotli will be prefered over gzip if the browser does not set a preference
1 parent 572b59f commit c43844b

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

package-lock.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
"react-cookie": "^2.1.4"
5050
},
5151
"dependencies": {
52+
"accept": "^3.0.2",
5253
"accept-language-parser": "^1.5.0",
5354
"babel-core": "^6.26.0",
5455
"babel-loader": "^7.1.4",
5556
"blessed": "^0.1.81",
57+
"brotli-webpack-plugin": "^0.5.0",
5658
"bundle-loader": "^0.5.6",
5759
"case-sensitive-paths-webpack-plugin": "^2.1.2",
5860
"chalk": "^1.1.3",
@@ -85,7 +87,6 @@
8587
"minimist": "^1.2.0",
8688
"mkdirp": "^0.5.1",
8789
"mz": "^2.7.0",
88-
"negotiator": "^0.6.1",
8990
"ngrok": "^2.2.25",
9091
"node-sass": "^4.8.3",
9192
"node-style-loader": "^0.0.1-alpha",

src/framework/server/setup-http-server/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33
import fs from 'mz/fs';
44
import express from 'express';
55
import mime from 'mime';
6-
import getPreferredEncodings from 'negotiator/lib/encoding';
6+
import accept from 'accept';
77
import compression from 'compression';
88
import cookiesMiddleware from 'universal-cookie-express';
99
import getWebpackSettings from '../../../shared/webpack-settings';
@@ -25,7 +25,7 @@ function redirectToPreCompressed(root, encodingTransforms = {}) {
2525
return async function redirect(req, res, next) {
2626
// req.acceptsEncoding is not powerful enough, and creates a new instance of Accept AND Negociator
2727
// on every single call. negotiator/lib/encoding.getPreferredEncodings is a pure function, I'm going to use that.
28-
const encodings = getPreferredEncodings(req.header('Accept-Encoding'), availableEncodings);
28+
const encodings = accept.encodings(req.header('Accept-Encoding'), availableEncodings);
2929

3030
for (const encoding of encodings) {
3131
const getCompressedPath = encodingTransforms[encoding];

src/internals/webpack/features/optimize.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import UglifyJsPlugin from 'uglifyjs-webpack-plugin';
44
import OfflinePlugin from 'offline-plugin';
5-
import CompressionPlugin from 'compression-webpack-plugin';
5+
import GzipPlugin from 'compression-webpack-plugin';
6+
import BrotliPlugin from 'brotli-webpack-plugin';
7+
68
import BaseFeature from '../BaseFeature';
79
import type WebpackConfigBuilder from '../WebpackConfigBuilder';
810

@@ -121,8 +123,15 @@ export default class OptimizeFeature extends BaseFeature {
121123

122124
config.injectPlugins([
123125

124-
// TODO brotli
125-
new CompressionPlugin([{
126+
new BrotliPlugin({
127+
asset: '[path].br[query]',
128+
test: /\.(js|css|html|svg)$/,
129+
threshold: 0,
130+
minRatio: 0.8,
131+
quality: 11,
132+
}),
133+
134+
new GzipPlugin([{
126135
asset: '[path].gz[query]',
127136
algorithm: 'gzip',
128137
test: /\.(js|css|html|svg)$/,
@@ -137,6 +146,7 @@ export default class OptimizeFeature extends BaseFeature {
137146
// this is applied before any match in `caches` section
138147
excludes: [
139148
'**/*.gz',
149+
'**/*.br',
140150
'**/*.map',
141151
'**/*.LICENSE',
142152
],

0 commit comments

Comments
 (0)