Skip to content

Commit 9ea280e

Browse files
author
Mikko Tiihonen
committed
Precompress and optimize static files during build and serve them to the browsers.
- create precompressed variants of js, css, svg files: gz using zopfli and br using brotli Currently using forked versions of expressjs/send/negotiator while waiting for the PRs to go through: - jshttp/negotiator#49 - pillarjs/send#108
1 parent aeb1502 commit 9ea280e

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"csscolorparser": "1.0.3",
6060
"debug": "2.3.3",
6161
"element-resize-detector": "1.1.9",
62-
"express": "4.14.0",
62+
"express": "gmokki/express#6eeb054",
6363
"fluxible": "1.2.0",
6464
"fluxible-action-utils": "0.2.4",
6565
"fluxible-addons-react": "0.2.8",
@@ -112,7 +112,9 @@
112112
"babel-loader": "6.2.8",
113113
"babel-plugin-dev-expression": "0.2.1",
114114
"babel-preset-latest": "6.16.0",
115+
"brotli-webpack-plugin": "0.1.1",
115116
"chance": "1.0.4",
117+
"compression-webpack-plugin": "0.3.2",
116118
"css-loader": "0.26.0",
117119
"csswring": "5.1.0",
118120
"eslint": "3.10.2",

server/server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ const app = express();
4242
function setUpStaticFolders() {
4343
const staticFolder = path.join(process.cwd(), '_static');
4444
// Sert cache for 1 week
45-
app.use(config.APP_PATH, express.static(staticFolder, { maxAge: 604800000 }));
45+
app.use(config.APP_PATH, express.static(staticFolder, {
46+
maxAge: 604800000,
47+
precompressed: [{ encoding: 'br', extension: '.br' }, { encoding: 'gzip', extension: '.gz' }],
48+
}));
4649
}
4750

4851
function setUpMiddleware() {

webpack.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const autoprefixer = require('autoprefixer');
99
const csswring = require('csswring');
1010
const StatsPlugin = require('stats-webpack-plugin');
1111
const fs = require('fs');
12+
const GzipCompressionPlugin = require("compression-webpack-plugin")
13+
const BrotliCompressionPlugin = require('brotli-webpack-plugin')
1214

1315
require('babel-core/register')({
1416
presets: ['modern-node', 'stage-2'], // eslint-disable-line prefer-template
@@ -153,6 +155,19 @@ function getPluginsConfig(env) {
153155
filename: 'css/[name].[contenthash].css',
154156
allChunks: true,
155157
}),
158+
new GzipCompressionPlugin({
159+
debug: true,
160+
asset: '[path].gz[query]',
161+
algorithm: 'zopfli',
162+
test: /\.(js|css|html|svg)$/,
163+
minRatio: 0.95
164+
}),
165+
new BrotliCompressionPlugin({
166+
debug: true,
167+
asset: '[path].br[query]',
168+
test: /\.(js|css|html|svg)$/,
169+
minRatio: 0.95
170+
}),
156171
new webpack.NoErrorsPlugin(),
157172
]);
158173
}

0 commit comments

Comments
 (0)