Skip to content

Commit 6d55853

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 6d55853

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const autoprefixer = require('autoprefixer');
99
const csswring = require('csswring');
1010
const StatsPlugin = require('stats-webpack-plugin');
1111
const fs = require('fs');
12+
const glob = require('glob');
13+
const GzipCompressionPlugin = require("compression-webpack-plugin")
14+
const BrotliCompressionPlugin = require('brotli-webpack-plugin')
1215

1316
require('babel-core/register')({
1417
presets: ['modern-node', 'stage-2'], // eslint-disable-line prefer-template
@@ -153,6 +156,19 @@ function getPluginsConfig(env) {
153156
filename: 'css/[name].[contenthash].css',
154157
allChunks: true,
155158
}),
159+
new GzipCompressionPlugin({
160+
debug: true,
161+
asset: '[path].gz[query]',
162+
algorithm: 'zopfli',
163+
test: /\.(js|css|html|svg)$/,
164+
minRatio: 0.95
165+
}),
166+
new BrotliCompressionPlugin({
167+
debug: true,
168+
asset: '[path].br[query]',
169+
test: /\.(js|css|html|svg)$/,
170+
minRatio: 0.95
171+
}),
156172
new webpack.NoErrorsPlugin(),
157173
]);
158174
}

0 commit comments

Comments
 (0)