From c427d5a81e6f637e3856f4fc72e4557206dbfc3a Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Wed, 4 Jan 2017 19:20:02 +0530 Subject: [PATCH 1/8] Add support for compiling with custom compression types. --- server/build/gzip.js | 24 ++-- server/render.js | 2 +- yarn.lock | 307 ++++++++++++++++++++++--------------------- 3 files changed, 172 insertions(+), 161 deletions(-) diff --git a/server/build/gzip.js b/server/build/gzip.js index c3ae485fbb734..708345e1e4745 100644 --- a/server/build/gzip.js +++ b/server/build/gzip.js @@ -2,9 +2,11 @@ import fs from 'fs' import path from 'path' import zlib from 'zlib' import glob from 'glob-promise' +import getConfig from '../config' export default async function gzipAssets (dir) { const nextDir = path.resolve(dir, '.next') + const config = getConfig(dir) const coreAssets = [ path.join(nextDir, 'commons.js'), @@ -22,17 +24,23 @@ export default async function gzipAssets (dir) { const currentChunk = allAssets.splice(0, 10) if (currentChunk.length === 0) break - await Promise.all(currentChunk.map(gzip)) + await Promise.all(currentChunk.map((f) => compress(config, f))) } } -export function gzip (filePath) { - const input = fs.createReadStream(filePath) - const output = fs.createWriteStream(`${filePath}.gz`) +export function compress (config, filePath) { + const compressionMap = config.compress || { + gzip: (f) => fs.createReadStream(f).pipe(zlib.createGzip()) + } - return new Promise((resolve, reject) => { - const stream = input.pipe(zlib.createGzip()).pipe(output) - stream.on('error', reject) - stream.on('finish', resolve) + const promises = Object.keys(compressionMap).map((type) => { + return new Promise((resolve, reject) => { + const output = fs.createWriteStream(`${filePath}.${type}`) + const stream = compressionMap[type](filePath).pipe(output) + stream.on('error', reject) + stream.on('finish', resolve) + }) }) + + return Promise.all(promises) } diff --git a/server/render.js b/server/render.js index c4c3b35760a7b..553c8d96c16e3 100644 --- a/server/render.js +++ b/server/render.js @@ -148,7 +148,7 @@ export async function serveStaticWithGzip (req, res, path) { } try { - const gzipPath = `${path}.gz` + const gzipPath = `${path}.gzip` res.setHeader('Content-Encoding', 'gzip') await serveStatic(req, res, gzipPath) } catch (ex) { diff --git a/yarn.lock b/yarn.lock index 5f5ed2c335f29..a6ff1f02fe62c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1,7 +1,5 @@ # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # yarn lockfile v1 - - abab@^1.0.0: version "1.0.3" resolved "https://registry.yarnpkg.com/abab/-/abab-1.0.3.tgz#b81de5f7274ec4e756d797cd834f303642724e5d" @@ -78,6 +76,10 @@ ansi-html@0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/ansi-html/-/ansi-html-0.0.6.tgz#bda8e33dd2ee1c20f54c08eb405713cbfc0ed80e" +ansi-html@0.0.7: + version "0.0.7" + resolved "https://registry.yarnpkg.com/ansi-html/-/ansi-html-0.0.7.tgz#813584021962a9e9e6fd039f940d12f56ca7859e" + ansi-regex@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-2.0.0.tgz#c5061b6e0ef8a81775e50f5d66151bf6bf371107" @@ -196,14 +198,14 @@ async-each@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d" -async@1.x, async@^1.3.0, async@^1.4.0, async@^1.4.2: - version "1.5.2" - resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" - async@^0.9.0: version "0.9.2" resolved "https://registry.yarnpkg.com/async/-/async-0.9.2.tgz#aea74d5e61c1f899613bf64bda66d4c78f2fd17d" +async@^1.3.0, async@^1.4.0, async@^1.4.2, async@1.x: + version "1.5.2" + resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" + async@^2.1.2, async@^2.1.4: version "2.1.4" resolved "https://registry.yarnpkg.com/async/-/async-2.1.4.tgz#2d2160c7788032e4dd6cbe2502f1f9a2c8f6cde4" @@ -234,7 +236,7 @@ babel-code-frame@^6.16.0, babel-code-frame@^6.20.0: esutils "^2.0.2" js-tokens "^2.0.0" -babel-core@6.21.0, babel-core@^6.0.0, babel-core@^6.0.2, babel-core@^6.18.0: +babel-core@^6.0.0, babel-core@^6.0.2, babel-core@^6.18.0, babel-core@6.21.0: version "6.21.0" resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.21.0.tgz#75525480c21c803f826ef3867d22c19f080a3724" dependencies: @@ -268,7 +270,7 @@ babel-eslint@7.1.1: babylon "^6.13.0" lodash.pickby "^4.6.0" -babel-generator@6.21.0, babel-generator@^6.18.0, babel-generator@^6.21.0: +babel-generator@^6.18.0, babel-generator@^6.21.0, babel-generator@6.21.0: version "6.21.0" resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.21.0.tgz#605f1269c489a1c75deeca7ea16d43d4656c8494" dependencies: @@ -493,7 +495,7 @@ babel-plugin-syntax-trailing-function-commas@^6.13.0: version "6.20.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-trailing-function-commas/-/babel-plugin-syntax-trailing-function-commas-6.20.0.tgz#442835e19179f45b87e92d477d70b9f1f18b5c4f" -babel-plugin-transform-async-to-generator@6.16.0, babel-plugin-transform-async-to-generator@^6.8.0: +babel-plugin-transform-async-to-generator@^6.8.0, babel-plugin-transform-async-to-generator@6.16.0: version "6.16.0" resolved "https://registry.yarnpkg.com/babel-plugin-transform-async-to-generator/-/babel-plugin-transform-async-to-generator-6.16.0.tgz#19ec36cb1486b59f9f468adfa42ce13908ca2999" dependencies: @@ -850,7 +852,7 @@ babel-register@^6.18.0: mkdirp "^0.5.1" source-map-support "^0.4.2" -babel-runtime@6.20.0, babel-runtime@^6.0.0, babel-runtime@^6.11.6, babel-runtime@^6.18.0, babel-runtime@^6.20.0, babel-runtime@^6.9.0, babel-runtime@^6.9.1: +babel-runtime@^6.0.0, babel-runtime@^6.11.6, babel-runtime@^6.18.0, babel-runtime@^6.20.0, babel-runtime@^6.9.0, babel-runtime@^6.9.1, babel-runtime@6.20.0: version "6.20.0" resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.20.0.tgz#87300bdcf4cd770f09bf0048c64204e17806d16f" dependencies: @@ -912,6 +914,10 @@ beeper@^1.0.0: version "1.1.1" resolved "https://registry.yarnpkg.com/beeper/-/beeper-1.1.1.tgz#e6d5ea8c5dad001304a70b22638447f69cb2f809" +benchmark@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-1.0.0.tgz#2f1e2fa4c359f11122aa183082218e957e390c73" + benchmark@2.1.3: version "2.1.3" resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-2.1.3.tgz#e10e40e4d53d0e1c9d77a834fde593994dca7f0c" @@ -919,10 +925,6 @@ benchmark@2.1.3: lodash "^4.17.3" platform "^1.3.3" -benchmark@~1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-1.0.0.tgz#2f1e2fa4c359f11122aa183082218e957e390c73" - big.js@^3.1.3: version "3.1.3" resolved "https://registry.yarnpkg.com/big.js/-/big.js-3.1.3.tgz#4cada2193652eb3ca9ec8e55c9015669c9806978" @@ -978,12 +980,6 @@ browser-resolve@^1.11.2: dependencies: resolve "1.1.7" -browserify-aes@0.4.0: - version "0.4.0" - resolved "https://registry.yarnpkg.com/browserify-aes/-/browserify-aes-0.4.0.tgz#067149b668df31c4b58533e02d01e806d8608e2c" - dependencies: - inherits "^2.0.1" - browserify-aes@^1.0.0, browserify-aes@^1.0.4: version "1.0.6" resolved "https://registry.yarnpkg.com/browserify-aes/-/browserify-aes-1.0.6.tgz#5e7725dbdef1fd5930d4ebab48567ce451c48a0a" @@ -994,6 +990,12 @@ browserify-aes@^1.0.0, browserify-aes@^1.0.4: evp_bytestokey "^1.0.0" inherits "^2.0.1" +browserify-aes@0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/browserify-aes/-/browserify-aes-0.4.0.tgz#067149b668df31c4b58533e02d01e806d8608e2c" + dependencies: + inherits "^2.0.1" + browserify-cipher@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/browserify-cipher/-/browserify-cipher-1.0.0.tgz#9988244874bf5ed4e28da95666dcd66ac8fc363a" @@ -1320,19 +1322,19 @@ create-hmac@^1.1.0, create-hmac@^1.1.2: create-hash "^1.1.0" inherits "^2.0.1" -cross-spawn@5.0.1: - version "5.0.1" - resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.0.1.tgz#a3bbb302db2297cbea3c04edf36941f4613aa399" +cross-spawn@^4: + version "4.0.2" + resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-4.0.2.tgz#7b9247621c23adfdd3856004a823cbe397424d41" dependencies: lru-cache "^4.0.1" - shebang-command "^1.2.0" which "^1.2.9" -cross-spawn@^4: - version "4.0.2" - resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-4.0.2.tgz#7b9247621c23adfdd3856004a823cbe397424d41" +cross-spawn@5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.0.1.tgz#a3bbb302db2297cbea3c04edf36941f4613aa399" dependencies: lru-cache "^4.0.1" + shebang-command "^1.2.0" which "^1.2.9" cryptiles@2.x.x: @@ -1341,15 +1343,6 @@ cryptiles@2.x.x: dependencies: boom "2.x.x" -crypto-browserify@3.3.0: - version "3.3.0" - resolved "https://registry.yarnpkg.com/crypto-browserify/-/crypto-browserify-3.3.0.tgz#b9fc75bb4a0ed61dcf1cd5dae96eb30c9c3e506c" - dependencies: - browserify-aes "0.4.0" - pbkdf2-compat "2.0.1" - ripemd160 "0.2.0" - sha.js "2.2.6" - crypto-browserify@^3.11.0: version "3.11.0" resolved "https://registry.yarnpkg.com/crypto-browserify/-/crypto-browserify-3.11.0.tgz#3652a0906ab9b2a7e0c3ce66a408e957a2485522" @@ -1365,7 +1358,16 @@ crypto-browserify@^3.11.0: public-encrypt "^4.0.0" randombytes "^2.0.0" -cssom@0.3.x, "cssom@>= 0.3.0 < 0.4.0": +crypto-browserify@3.3.0: + version "3.3.0" + resolved "https://registry.yarnpkg.com/crypto-browserify/-/crypto-browserify-3.3.0.tgz#b9fc75bb4a0ed61dcf1cd5dae96eb30c9c3e506c" + dependencies: + browserify-aes "0.4.0" + pbkdf2-compat "2.0.1" + ripemd160 "0.2.0" + sha.js "2.2.6" + +"cssom@>= 0.3.0 < 0.4.0", cssom@0.3.x: version "0.3.1" resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.1.tgz#c9e37ef2490e64f6d1baa10fda852257082c25d3" @@ -1453,7 +1455,7 @@ deglob@^2.0.0: run-parallel "^1.1.2" uniq "^1.0.1" -del@2.2.2, del@^2.0.2: +del@^2.0.2, del@2.2.2: version "2.2.2" resolved "https://registry.yarnpkg.com/del/-/del-2.2.2.tgz#c12c981d067846c84bcaf862cff930d907ffd1a8" dependencies: @@ -1593,7 +1595,7 @@ enhanced-resolve@~0.9.0: memory-fs "^0.2.0" tapable "^0.1.8" -"errno@>=0.1.1 <0.2.0-0", errno@^0.1.3: +errno@^0.1.3, "errno@>=0.1.1 <0.2.0-0": version "0.1.4" resolved "https://registry.yarnpkg.com/errno/-/errno-0.1.4.tgz#b896e23a9e5e8ba33871fc996abd3635fc9a1c7d" dependencies: @@ -1664,7 +1666,7 @@ es6-set@~0.1.3: es6-symbol "3" event-emitter "~0.3.4" -es6-symbol@3, es6-symbol@~3.1, es6-symbol@~3.1.0: +es6-symbol@~3.1, es6-symbol@~3.1.0, es6-symbol@3: version "3.1.0" resolved "https://registry.yarnpkg.com/es6-symbol/-/es6-symbol-3.1.0.tgz#94481c655e7a7cad82eba832d97d5433496d7ffa" dependencies: @@ -1688,7 +1690,7 @@ escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" -escodegen@1.8.x, escodegen@^1.6.1: +escodegen@^1.6.1, escodegen@1.8.x: version "1.8.1" resolved "https://registry.yarnpkg.com/escodegen/-/escodegen-1.8.1.tgz#5a5b53af4693110bebb0867aa3430dd3b70a1018" dependencies: @@ -1777,7 +1779,7 @@ espree@^3.3.1: acorn "^4.0.1" acorn-jsx "^3.0.0" -esprima@2.7.x, esprima@^2.6.0, esprima@^2.7.1: +esprima@^2.6.0, esprima@^2.7.1, esprima@2.7.x: version "2.7.3" resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581" @@ -2185,12 +2187,6 @@ glob-watcher@^0.0.6: dependencies: gaze "^0.5.1" -glob2base@^0.0.12: - version "0.0.12" - resolved "https://registry.yarnpkg.com/glob2base/-/glob2base-0.0.12.tgz#9d419b3e28f12e83a362164a277055922c9c0d56" - dependencies: - find-index "^0.1.1" - glob@^4.3.1: version "4.5.3" resolved "https://registry.yarnpkg.com/glob/-/glob-4.5.3.tgz#c6cb73d3226c1efef04de3c56d012f03377ee15f" @@ -2229,6 +2225,12 @@ glob@~3.1.21: inherits "1" minimatch "~0.2.11" +glob2base@^0.0.12: + version "0.0.12" + resolved "https://registry.yarnpkg.com/glob2base/-/glob2base-0.0.12.tgz#9d419b3e28f12e83a362164a277055922c9c0d56" + dependencies: + find-index "^0.1.1" + global-modules@^0.2.3: version "0.2.3" resolved "https://registry.yarnpkg.com/global-modules/-/global-modules-0.2.3.tgz#ea5a3bed42c6d6ce995a4f8a1269b5dae223828d" @@ -2527,14 +2529,14 @@ husky@0.12.0: is-ci "^1.0.9" normalize-path "^1.0.0" -iconv-lite@0.4.13: - version "0.4.13" - resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2" - iconv-lite@^0.4.13, iconv-lite@~0.4.13: version "0.4.15" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.15.tgz#fe265a218ac6a57cfe854927e9d04c19825eddeb" +iconv-lite@0.4.13: + version "0.4.13" + resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2" + ieee754@^1.1.4: version "1.1.8" resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.8.tgz#be33d40ac10ef1926701f6f08a2d86fbfd1ad3e4" @@ -2558,14 +2560,14 @@ inflight@^1.0.4: once "^1.3.0" wrappy "1" +inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.0, inherits@~2.0.1, inherits@2, inherits@2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" + inherits@1: version "1.0.2" resolved "https://registry.yarnpkg.com/inherits/-/inherits-1.0.2.tgz#ca4309dadee6b54cc0b8d247e8d7c7a0975bdc9b" -inherits@2, inherits@2.0.3, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.0, inherits@~2.0.1: - version "2.0.3" - resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" - inherits@2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.1.tgz#b17d08d326b4423e568eff719f91b0b1cbdf69f1" @@ -2784,14 +2786,14 @@ is@^3.1.0: version "3.2.0" resolved "https://registry.yarnpkg.com/is/-/is-3.2.0.tgz#a362e3daf7df3fd8b7114115d624c5b7e1cb90f7" +isarray@^1.0.0, isarray@~1.0.0, isarray@1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" + isarray@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf" -isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" - isexe@^1.1.1: version "1.1.2" resolved "https://registry.yarnpkg.com/isexe/-/isexe-1.1.2.tgz#36f3e22e60750920f5e7241a476a8c6a42275ad0" @@ -3255,14 +3257,14 @@ js-tokens@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-2.0.0.tgz#79903f5563ee778cc1162e6dcf1a0027c97f9cb5" -js-yaml@3.6.1, js-yaml@^3.5.1: +js-yaml@^3.5.1, js-yaml@3.6.1: version "3.6.1" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.6.1.tgz#6e5fe67d8b205ce4d22fad05b7781e8dadcc4b30" dependencies: argparse "^1.0.7" esprima "^2.6.0" -js-yaml@3.x, js-yaml@^3.7.0: +js-yaml@^3.7.0, js-yaml@3.x: version "3.7.0" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.7.0.tgz#5c967ddd837a9bfdca5f2de84253abe8a1c03b80" dependencies: @@ -3306,7 +3308,7 @@ jsesc@~0.5.0: version "0.5.0" resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-0.5.0.tgz#e7dee66e35d6fc16f710fe91d5cf69f70f08911d" -json-loader@0.5.4, json-loader@^0.5.4: +json-loader@^0.5.4, json-loader@0.5.4: version "0.5.4" resolved "https://registry.yarnpkg.com/json-loader/-/json-loader-0.5.4.tgz#8baa1365a632f58a3c46d20175fc6002c96e37de" @@ -3406,7 +3408,7 @@ loader-runner@^2.2.0: version "2.2.0" resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-2.2.0.tgz#824c1b699c4e7a2b6501b85902d5b862bf45b3fa" -loader-utils@0.2.16, loader-utils@^0.2.11, loader-utils@^0.2.16: +loader-utils@^0.2.11, loader-utils@^0.2.16, loader-utils@0.2.16: version "0.2.16" resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-0.2.16.tgz#f08632066ed8282835dff88dfb52704765adee6d" dependencies: @@ -3609,10 +3611,6 @@ loose-envify@^1.0.0, loose-envify@^1.1.0: dependencies: js-tokens "^2.0.0" -lru-cache@2: - version "2.7.3" - resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-2.7.3.tgz#6d4524e8b955f95d4f5b58851ce21dd72fb4e952" - lru-cache@^4.0.1: version "4.0.2" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.0.2.tgz#1d17679c069cda5d040991a09dbc2c0db377e55e" @@ -3620,6 +3618,10 @@ lru-cache@^4.0.1: pseudomap "^1.0.1" yallist "^2.0.0" +lru-cache@2: + version "2.7.3" + resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-2.7.3.tgz#6d4524e8b955f95d4f5b58851ce21dd72fb4e952" + makeerror@1.0.x: version "1.0.11" resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c" @@ -3717,7 +3719,7 @@ mime-types@^2.1.11, mime-types@^2.1.12, mime-types@~2.1.11, mime-types@~2.1.7: dependencies: mime-db "~1.25.0" -mime@1.3.4, mime@^1.3.4: +mime@^1.3.4, mime@1.3.4: version "1.3.4" resolved "https://registry.yarnpkg.com/mime/-/mime-1.3.4.tgz#115f9e3b6b3daf2959983cb38f149a2d40eb5d53" @@ -3731,18 +3733,18 @@ minimalistic-assert@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.0.tgz#702be2dda6b37f4836bcb3f5db56641b64a1d3d3" -"minimatch@2 || 3", minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3: - version "3.0.3" - resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.3.tgz#2a4e4090b96b2db06a9d7df01055a62a77c9b774" - dependencies: - brace-expansion "^1.0.0" - minimatch@^2.0.1: version "2.0.10" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-2.0.10.tgz#8d087c39c6b38c001b97fca7ce6d0e1e80afbac7" dependencies: brace-expansion "^1.0.0" +minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3, "minimatch@2 || 3": + version "3.0.3" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.3.tgz#2a4e4090b96b2db06a9d7df01055a62a77c9b774" + dependencies: + brace-expansion "^1.0.0" + minimatch@~0.2.11: version "0.2.14" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-0.2.14.tgz#c74e780574f63c6f9a090e90efbe6ef53a6a756a" @@ -3750,14 +3752,14 @@ minimatch@~0.2.11: lru-cache "2" sigmund "~1.0.0" -minimist@0.0.8, minimist@~0.0.1: - version "0.0.8" - resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" - -minimist@1.2.0, minimist@^1.1.0, minimist@^1.1.1, minimist@^1.2.0: +minimist@^1.1.0, minimist@^1.1.1, minimist@^1.2.0, minimist@1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" +minimist@~0.0.1, minimist@0.0.8: + version "0.0.8" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" + mkdirp-then@1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/mkdirp-then/-/mkdirp-then-1.2.0.tgz#a492c879ca4d873f5ee45008f8f55fd0150de3c5" @@ -3765,7 +3767,7 @@ mkdirp-then@1.2.0: any-promise "^1.1.0" mkdirp "^0.5.0" -mkdirp@0.5.x, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1: +mkdirp@^0.5.0, mkdirp@^0.5.1, "mkdirp@>=0.5 0", mkdirp@~0.5.0, mkdirp@~0.5.1, mkdirp@0.5.x: version "0.5.1" resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" dependencies: @@ -3935,7 +3937,7 @@ node.extend@^1.1.3: dependencies: is "^3.1.0" -nopt@3.x, nopt@~3.0.6: +nopt@~3.0.6, nopt@3.x: version "3.0.6" resolved "https://registry.yarnpkg.com/nopt/-/nopt-3.0.6.tgz#c6465dbf08abcd4db359317f79ac68a646b28ff9" dependencies: @@ -4045,7 +4047,7 @@ on-finished@~2.3.0: dependencies: ee-first "1.1.1" -once@1.x, once@^1.3.0, once@^1.4.0: +once@^1.3.0, once@^1.4.0, once@1.x: version "1.4.0" resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" dependencies: @@ -4323,7 +4325,7 @@ public-encrypt@^4.0.0: parse-asn1 "^5.0.0" randombytes "^2.0.1" -punycode@1.3.2, punycode@^1.2.4: +punycode@^1.2.4, punycode@1.3.2: version "1.3.2" resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d" @@ -4347,7 +4349,7 @@ querystring-es3@^0.2.0: version "0.2.1" resolved "https://registry.yarnpkg.com/querystring-es3/-/querystring-es3-0.2.1.tgz#9ec61f79049875707d69414596fd907a4d711e73" -querystring@0.2.0, querystring@^0.2.0: +querystring@^0.2.0, querystring@0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620" @@ -4427,15 +4429,6 @@ read-pkg@^1.0.0: normalize-package-data "^2.3.2" path-type "^1.0.0" -"readable-stream@>=1.0.33-1 <1.1.0-0": - version "1.0.34" - resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c" - dependencies: - core-util-is "~1.0.0" - inherits "~2.0.1" - isarray "0.0.1" - string_decoder "~0.10.x" - "readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@^2.1.0, readable-stream@^2.1.5, readable-stream@^2.2.2: version "2.2.2" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.2.tgz#a9e6fec3c7dda85f8bb1b3ba7028604556fc825e" @@ -4448,6 +4441,15 @@ read-pkg@^1.0.0: string_decoder "~0.10.x" util-deprecate "~1.0.1" +"readable-stream@>=1.0.33-1 <1.1.0-0": + version "1.0.34" + resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c" + dependencies: + core-util-is "~1.0.0" + inherits "~2.0.1" + isarray "0.0.1" + string_decoder "~0.10.x" + readable-stream@~1.1.9: version "1.1.14" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.1.14.tgz#7cf4c54ef648e3813084c636dd2079e166c081d9" @@ -4575,7 +4577,7 @@ replace-ext@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/replace-ext/-/replace-ext-0.0.1.tgz#29bbd92078a739f0bcce2b4ee41e837953522924" -request@2.75.0, request@^2.55.0: +request@^2.55.0, request@2.75.0: version "2.75.0" resolved "https://registry.yarnpkg.com/request/-/request-2.75.0.tgz#d2b8268a286da13eaa5d01adf5d18cc90f657d93" dependencies: @@ -4656,14 +4658,14 @@ resolve-from@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-2.0.0.tgz#9480ab20e94ffa1d9e80a804c7ea147611966b57" -resolve@1.1.7, resolve@1.1.x: - version "1.1.7" - resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b" - resolve@^1.1.6, resolve@^1.1.7, resolve@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.2.0.tgz#9589c3f2f6149d1417a40becc1663db6ec6bc26c" +resolve@1.1.7, resolve@1.1.x: + version "1.1.7" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b" + restore-cursor@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-1.0.1.tgz#34661f46886327fed2991479152252df92daa541" @@ -4677,20 +4679,20 @@ right-align@^0.1.1: dependencies: align-text "^0.1.1" -rimraf@2, rimraf@^2.2.8, rimraf@^2.3.3, rimraf@^2.4.3, rimraf@^2.4.4, rimraf@^2.5.4, rimraf@~2.5.1, rimraf@~2.5.4: +rimraf@^2.2.8, rimraf@^2.3.3, rimraf@^2.4.3, rimraf@^2.4.4, rimraf@^2.5.4, rimraf@~2.5.1, rimraf@~2.5.4, rimraf@2: version "2.5.4" resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.5.4.tgz#96800093cbf1a0c86bd95b4625467535c29dfa04" dependencies: glob "^7.0.5" -ripemd160@0.2.0: - version "0.2.0" - resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-0.2.0.tgz#2bf198bde167cacfa51c0a928e84b68bbe171fce" - ripemd160@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-1.0.1.tgz#93a4bbd4942bc574b69a8fa57c71de10ecca7d6e" +ripemd160@0.2.0: + version "0.2.0" + resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-0.2.0.tgz#2bf198bde167cacfa51c0a928e84b68bbe171fce" + run-async@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/run-async/-/run-async-0.1.0.tgz#c8ad4a5e110661e402a7d21b530e009f25f8e389" @@ -4727,14 +4729,14 @@ sax@^1.1.4: version "1.2.1" resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.1.tgz#7b8e656190b228e81a66aea748480d828cd2d37a" -"semver@2 || 3 || 4 || 5", semver@^5.1.0, semver@^5.3.0, semver@~5.3.0: - version "5.3.0" - resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f" - semver@^4.1.0: version "4.3.6" resolved "https://registry.yarnpkg.com/semver/-/semver-4.3.6.tgz#300bc6e0e86374f7ba61068b5b1ecd57fc6532da" +semver@^5.1.0, semver@^5.3.0, semver@~5.3.0, "semver@2 || 3 || 4 || 5": + version "5.3.0" + resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f" + send@0.14.1: version "0.14.1" resolved "https://registry.yarnpkg.com/send/-/send-0.14.1.tgz#a954984325392f51532a7760760e459598c89f7a" @@ -4773,16 +4775,16 @@ setprototypeof@1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.0.2.tgz#81a552141ec104b88e89ce383103ad5c66564d08" -sha.js@2.2.6: - version "2.2.6" - resolved "https://registry.yarnpkg.com/sha.js/-/sha.js-2.2.6.tgz#17ddeddc5f722fb66501658895461977867315ba" - sha.js@^2.3.6: version "2.4.8" resolved "https://registry.yarnpkg.com/sha.js/-/sha.js-2.4.8.tgz#37068c2c476b6baf402d14a49c67f597921f634f" dependencies: inherits "^2.0.1" +sha.js@2.2.6: + version "2.2.6" + resolved "https://registry.yarnpkg.com/sha.js/-/sha.js-2.2.6.tgz#17ddeddc5f722fb66501658895461977867315ba" + shebang-command@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea" @@ -4839,7 +4841,7 @@ source-list-map@~0.1.0, source-list-map@~0.1.7: version "0.1.7" resolved "https://registry.yarnpkg.com/source-list-map/-/source-list-map-0.1.7.tgz#d4b5ce2a46535c72c7e8527c71a77d250618172e" -source-map-support@0.4.8, source-map-support@^0.4.2: +source-map-support@^0.4.2, source-map-support@0.4.8: version "0.4.8" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.8.tgz#4871918d8a3af07289182e974e32844327b2e98b" dependencies: @@ -4961,6 +4963,10 @@ stream-http@^2.3.1: to-arraybuffer "^1.0.0" xtend "^4.0.0" +string_decoder@^0.10.25, string_decoder@~0.10.x: + version "0.10.31" + resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94" + string-hash@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/string-hash/-/string-hash-1.1.1.tgz#8e85bed291e0763b8f6809d9c3368fea048db3dc" @@ -4984,15 +4990,11 @@ string.prototype.codepointat@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/string.prototype.codepointat/-/string.prototype.codepointat-0.2.0.tgz#6b26e9bd3afcaa7be3b4269b526de1b82000ac78" -string_decoder@^0.10.25, string_decoder@~0.10.x: - version "0.10.31" - resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94" - stringstream@~0.0.4: version "0.0.5" resolved "https://registry.yarnpkg.com/stringstream/-/stringstream-0.0.5.tgz#4e484cd4de5a0bbbee18e46307710a8a81621878" -strip-ansi@3.0.1, strip-ansi@^3.0.0, strip-ansi@^3.0.1: +strip-ansi@^3.0.0, strip-ansi@^3.0.1, strip-ansi@3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf" dependencies: @@ -5127,6 +5129,10 @@ throat@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/throat/-/throat-3.0.0.tgz#e7c64c867cbb3845f10877642f7b60055b8ec0d6" +through@^2.3.6, through@^2.3.8: + version "2.3.8" + resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" + through2@^0.6.1, through2@^0.6.3, through2@~0.6.3: version "0.6.5" resolved "https://registry.yarnpkg.com/through2/-/through2-0.6.5.tgz#41ab9c67b29d57209071410e1d7a7a968cd3ad48" @@ -5141,10 +5147,6 @@ through2@^2.0.0, through2@^2.0.1: readable-stream "^2.1.5" xtend "~4.0.1" -through@^2.3.6, through@^2.3.8: - version "2.3.8" - resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" - tildify@^1.0.0: version "1.2.0" resolved "https://registry.yarnpkg.com/tildify/-/tildify-1.2.0.tgz#dcec03f55dca9b7aa3e5b04f21817eb56e63588a" @@ -5242,7 +5244,7 @@ unique-stream@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/unique-stream/-/unique-stream-1.0.0.tgz#d59a4a75427447d9aa6c91e70263f8d26a4b104b" -url@0.11.0, url@^0.11.0: +url@^0.11.0, url@0.11.0: version "0.11.0" resolved "https://registry.yarnpkg.com/url/-/url-0.11.0.tgz#3838e97cfc60521eb73c525a8e55bfdd9e2e28f1" dependencies: @@ -5263,7 +5265,7 @@ util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" -util@0.10.3, util@^0.10.3: +util@^0.10.3, util@0.10.3: version "0.10.3" resolved "https://registry.yarnpkg.com/util/-/util-0.10.3.tgz#7afb1afe50805246489e3db7fe0ed379336ac0f9" dependencies: @@ -5414,6 +5416,26 @@ webpack-stream@3.2.0: vinyl "^1.1.0" webpack "^1.12.9" +webpack@^1.12.9: + version "1.14.0" + resolved "https://registry.yarnpkg.com/webpack/-/webpack-1.14.0.tgz#54f1ffb92051a328a5b2057d6ae33c289462c823" + dependencies: + acorn "^3.0.0" + async "^1.3.0" + clone "^1.0.2" + enhanced-resolve "~0.9.0" + interpret "^0.6.4" + loader-utils "^0.2.11" + memory-fs "~0.3.0" + mkdirp "~0.5.0" + node-libs-browser "^0.7.0" + optimist "~0.6.0" + supports-color "^3.1.0" + tapable "~0.1.8" + uglify-js "~2.7.3" + watchpack "^0.2.1" + webpack-core "~0.6.9" + webpack@2.2.0-rc.3: version "2.2.0-rc.3" resolved "https://registry.yarnpkg.com/webpack/-/webpack-2.2.0-rc.3.tgz#ac072c06c88aae75abdfd33510e7c5fd965f843f" @@ -5440,26 +5462,6 @@ webpack@2.2.0-rc.3: webpack-sources "^0.1.0" yargs "^6.0.0" -webpack@^1.12.9: - version "1.14.0" - resolved "https://registry.yarnpkg.com/webpack/-/webpack-1.14.0.tgz#54f1ffb92051a328a5b2057d6ae33c289462c823" - dependencies: - acorn "^3.0.0" - async "^1.3.0" - clone "^1.0.2" - enhanced-resolve "~0.9.0" - interpret "^0.6.4" - loader-utils "^0.2.11" - memory-fs "~0.3.0" - mkdirp "~0.5.0" - node-libs-browser "^0.7.0" - optimist "~0.6.0" - supports-color "^3.1.0" - tapable "~0.1.8" - uglify-js "~2.7.3" - watchpack "^0.2.1" - webpack-core "~0.6.9" - whatwg-encoding@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/whatwg-encoding/-/whatwg-encoding-1.0.1.tgz#3c6c451a198ee7aec55b1ec61d0920c67801a5f4" @@ -5493,17 +5495,13 @@ wide-align@^1.1.0: dependencies: string-width "^1.0.1" -window-size@0.1.0: - version "0.1.0" - resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d" - window-size@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.2.0.tgz#b4315bb4214a3d7058ebeee892e13fa24d98b075" -wordwrap@0.0.2: - version "0.0.2" - resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f" +window-size@0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d" wordwrap@^1.0.0, wordwrap@~1.0.0: version "1.0.0" @@ -5513,6 +5511,10 @@ wordwrap@~0.0.2: version "0.0.3" resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107" +wordwrap@0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f" + worker-farm@^1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/worker-farm/-/worker-farm-1.3.1.tgz#4333112bb49b17aa050b87895ca6b2cacf40e5ff" @@ -5559,7 +5561,7 @@ write@^0.2.1: version "2.0.1" resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-2.0.1.tgz#4d8b8f1eccd3419aa362061becef515e1e559635" -"xtend@>=4.0.0 <4.1.0-0", xtend@^4.0.0, xtend@^4.0.1, xtend@~4.0.1: +xtend@^4.0.0, xtend@^4.0.1, "xtend@>=4.0.0 <4.1.0-0", xtend@~4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af" @@ -5604,3 +5606,4 @@ yargs@~3.10.0: cliui "^2.1.0" decamelize "^1.0.0" window-size "0.1.0" + From 9b60143c7c72721030a48dd0374f556f91dd4cb7 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Wed, 4 Jan 2017 20:55:37 +0530 Subject: [PATCH 2/8] Add support for serving custom compress types as well. --- server/index.js | 14 ++++++++++---- server/render.js | 14 +++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/server/index.js b/server/index.js index f50c6779c1a44..00e02edb54793 100644 --- a/server/index.js +++ b/server/index.js @@ -22,11 +22,17 @@ export default class Server { this.dir = resolve(dir) this.dev = dev this.quiet = quiet - this.renderOpts = { dir: this.dir, dev, staticMarkup } this.router = new Router() this.hotReloader = dev ? new HotReloader(this.dir, { quiet }) : null this.http = null this.config = getConfig(this.dir) + this.compressTypes = Object.keys(this.config.compress || { gzip: true }) + this.renderOpts = { + dir: this.dir, + compressTypes: this.compressTypes, + dev, + staticMarkup + } this.defineRoutes() } @@ -62,12 +68,12 @@ export default class Server { this.router.get('/_next/main.js', async (req, res, params) => { const p = join(this.dir, '.next/main.js') - await serveStaticWithGzip(req, res, p) + await serveStaticWithGzip(req, res, p, this.compressTypes) }) this.router.get('/_next/commons.js', async (req, res, params) => { const p = join(this.dir, '.next/commons.js') - await serveStaticWithGzip(req, res, p) + await serveStaticWithGzip(req, res, p, this.compressTypes) }) this.router.get('/_next/pages/:path*', async (req, res, params) => { @@ -215,7 +221,7 @@ export default class Server { async serveStaticWithGzip (req, res, path) { this._serveStatic(req, res, () => { - return serveStaticWithGzip(req, res, path) + return serveStaticWithGzip(req, res, path, this.compressTypes) }) } diff --git a/server/render.js b/server/render.js index 553c8d96c16e3..f344a11ccc81b 100644 --- a/server/render.js +++ b/server/render.js @@ -96,9 +96,9 @@ async function doRender (req, res, pathname, query, { return '' + renderToStaticMarkup(doc) } -export async function renderJSON (req, res, page, { dir = process.cwd() } = {}) { +export async function renderJSON (req, res, page, { dir = process.cwd(), compressTypes } = {}) { const pagePath = await resolvePath(join(dir, '.next', 'bundles', 'pages', page)) - return serveStaticWithGzip(req, res, pagePath) + return serveStaticWithGzip(req, res, pagePath, compressTypes) } export async function renderErrorJSON (err, req, res, { dir = process.cwd(), dev = false } = {}) { @@ -141,15 +141,15 @@ function errorToJSON (err) { return json } -export async function serveStaticWithGzip (req, res, path) { - const encoding = accepts(req).encodings(['gzip']) - if (encoding !== 'gzip') { +export async function serveStaticWithGzip (req, res, path, compressTypes) { + const encoding = accepts(req).encodings(compressTypes) + if (!encoding) { return serveStatic(req, res, path) } try { - const gzipPath = `${path}.gzip` - res.setHeader('Content-Encoding', 'gzip') + const gzipPath = `${path}.${encoding}` + res.setHeader('Content-Encoding', encoding) await serveStatic(req, res, gzipPath) } catch (ex) { if (ex.code === 'ENOENT') { From 0274b23a52e9278e9aaf8a95fe3bdd4a885e6fea Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Wed, 4 Jan 2017 22:44:15 +0530 Subject: [PATCH 3/8] Fix a typo. --- server/index.js | 10 +++++----- server/render.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/server/index.js b/server/index.js index 00e02edb54793..4a46524b947f7 100644 --- a/server/index.js +++ b/server/index.js @@ -8,7 +8,7 @@ import { renderErrorJSON, sendHTML, serveStatic, - serveStaticWithGzip + serveStaticWithCompression } from './render' import Router from './router' import HotReloader from './hot-reloader' @@ -68,12 +68,12 @@ export default class Server { this.router.get('/_next/main.js', async (req, res, params) => { const p = join(this.dir, '.next/main.js') - await serveStaticWithGzip(req, res, p, this.compressTypes) + await serveStaticWithCompression(req, res, p, this.compressTypes) }) this.router.get('/_next/commons.js', async (req, res, params) => { const p = join(this.dir, '.next/commons.js') - await serveStaticWithGzip(req, res, p, this.compressTypes) + await serveStaticWithCompression(req, res, p, this.compressTypes) }) this.router.get('/_next/pages/:path*', async (req, res, params) => { @@ -219,9 +219,9 @@ export default class Server { return renderErrorJSON(err, req, res, this.renderOpts) } - async serveStaticWithGzip (req, res, path) { + async serveStaticWithCompression (req, res, path) { this._serveStatic(req, res, () => { - return serveStaticWithGzip(req, res, path, this.compressTypes) + return serveStaticWithCompression(req, res, path, this.compressTypes) }) } diff --git a/server/render.js b/server/render.js index f344a11ccc81b..87698a0d5518f 100644 --- a/server/render.js +++ b/server/render.js @@ -98,7 +98,7 @@ async function doRender (req, res, pathname, query, { export async function renderJSON (req, res, page, { dir = process.cwd(), compressTypes } = {}) { const pagePath = await resolvePath(join(dir, '.next', 'bundles', 'pages', page)) - return serveStaticWithGzip(req, res, pagePath, compressTypes) + return serveStaticWithCompression(req, res, pagePath, compressTypes) } export async function renderErrorJSON (err, req, res, { dir = process.cwd(), dev = false } = {}) { @@ -141,7 +141,7 @@ function errorToJSON (err) { return json } -export async function serveStaticWithGzip (req, res, path, compressTypes) { +export async function serveStaticWithCompression (req, res, path, compressTypes) { const encoding = accepts(req).encodings(compressTypes) if (!encoding) { return serveStatic(req, res, path) From 4adbf0c087d3ae7ecada6bd8601c842007fefc48 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Wed, 4 Jan 2017 23:35:22 +0530 Subject: [PATCH 4/8] Add an example app and add docs to README. --- README.md | 36 +++++++++++++++++++ .../with-custom-compress-encodings/README.md | 29 +++++++++++++++ .../components/Counter.js | 19 ++++++++++ .../components/Header.js | 19 ++++++++++ .../next.config.js | 19 ++++++++++ .../package.json | 17 +++++++++ .../pages/about.js | 10 ++++++ .../pages/index.js | 10 ++++++ server/index.js | 10 +++--- server/render.js | 10 +++--- 10 files changed, 170 insertions(+), 9 deletions(-) create mode 100644 examples/with-custom-compress-encodings/README.md create mode 100644 examples/with-custom-compress-encodings/components/Counter.js create mode 100644 examples/with-custom-compress-encodings/components/Header.js create mode 100644 examples/with-custom-compress-encodings/next.config.js create mode 100644 examples/with-custom-compress-encodings/package.json create mode 100644 examples/with-custom-compress-encodings/pages/about.js create mode 100644 examples/with-custom-compress-encodings/pages/index.js diff --git a/README.md b/README.md index 5f9bae7709b1d..42321f41840b1 100644 --- a/README.md +++ b/README.md @@ -519,6 +519,42 @@ Here's an example `.babelrc` file: } ``` +### Customizing compression encodings + + +

+ Examples + +

+ +By default Next.js will compress your core JavaScript assets for `gzip` encoding while it builds(with `next build`) the app. +If you need to add support for more encoding, you could simply add it via `next.config.js`. + +Here's an example config which adds supports for both `br` and `gzip` encodings. + +```js +var fs = require('fs') +var zlib = require('zlib') +var iltorb = require('iltorb') + +module.exports = { + // Return a stream of compressed file for each of the encodings you want + // + // The first listed encoding has the higher priority over others. + // In this case, it'll try to serve the `br` version if the browser supports it. + // Otherwise, it'll server gzipped version. + compress: { + br: function(filePath) { + return fs.createReadStream(filePath).pipe(iltorb.compressStream()) + }, + gzip: function(filePath) { + return fs.createReadStream(filePath).pipe(zlib.createGzip()) + } + } +} + +``` + ## Production deployment To deploy, instead of running `next`, you probably want to build ahead of time. Therefore, building and starting are separate commands: diff --git a/examples/with-custom-compress-encodings/README.md b/examples/with-custom-compress-encodings/README.md new file mode 100644 index 0000000000000..f90c4e68e86b7 --- /dev/null +++ b/examples/with-custom-compress-encodings/README.md @@ -0,0 +1,29 @@ +# Example app with custom compress encodings + +## How to use + +Download the example (or clone the repo)[https://github.com/zeit/next.js.git]: + +```bash +curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-custom-compress-encodings +cd with-custom-compress-encodings +``` + +Install it and run: + +```bash +npm install +npm run dev +``` + +Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)) + +```bash +now +``` + +## The idea behind the example + +By default, Next.js compile your assets with `gzip` compression. But if you want to add support more encodings like `br` or `deflate` you can do it very easily. + +This example shows how to add support for `br` and `gzip` compress encodings. For that it uses a config in `next.config.js`. diff --git a/examples/with-custom-compress-encodings/components/Counter.js b/examples/with-custom-compress-encodings/components/Counter.js new file mode 100644 index 0000000000000..959d8ae96f22d --- /dev/null +++ b/examples/with-custom-compress-encodings/components/Counter.js @@ -0,0 +1,19 @@ +import React from 'react' + +let count = 0 + +export default class Counter extends React.Component { + add () { + count += 1 + this.forceUpdate() + } + + render () { + return ( +
+

Count is: {count}

+ +
+ ) + } +} diff --git a/examples/with-custom-compress-encodings/components/Header.js b/examples/with-custom-compress-encodings/components/Header.js new file mode 100644 index 0000000000000..4409ca631f058 --- /dev/null +++ b/examples/with-custom-compress-encodings/components/Header.js @@ -0,0 +1,19 @@ +import Link from 'next/link' + +export default () => ( +
+ + Home + + + + About + +
+) + +const styles = { + a: { + marginRight: 10 + } +} diff --git a/examples/with-custom-compress-encodings/next.config.js b/examples/with-custom-compress-encodings/next.config.js new file mode 100644 index 0000000000000..4127922955aa3 --- /dev/null +++ b/examples/with-custom-compress-encodings/next.config.js @@ -0,0 +1,19 @@ +var fs = require('fs') +var zlib = require('zlib') +var iltorb = require('iltorb') + +module.exports = { + // Return a stream of compressed file for each of the encodings you want + // + // The first listed encoding has the higher priority over others. + // In this case, it'll try to serve the `br` version if the browser supports it. + // Otherwise, it'll server gzipped version. + compress: { + br: function (filePath) { + return fs.createReadStream(filePath).pipe(iltorb.compressStream()) + }, + gzip: function (filePath) { + return fs.createReadStream(filePath).pipe(zlib.createGzip()) + } + } +} diff --git a/examples/with-custom-compress-encodings/package.json b/examples/with-custom-compress-encodings/package.json new file mode 100644 index 0000000000000..9f649e1a9889c --- /dev/null +++ b/examples/with-custom-compress-encodings/package.json @@ -0,0 +1,17 @@ +{ + "name": "shared-modules", + "version": "1.0.0", + "description": "This example features:", + "main": "index.js", + "scripts": { + "dev": "next", + "build": "next build", + "start": "next start" + }, + "dependencies": { + "iltorb": "^1.0.13", + "next": "^2.0.0-beta" + }, + "author": "", + "license": "ISC" +} diff --git a/examples/with-custom-compress-encodings/pages/about.js b/examples/with-custom-compress-encodings/pages/about.js new file mode 100644 index 0000000000000..0daf4e1aed3ff --- /dev/null +++ b/examples/with-custom-compress-encodings/pages/about.js @@ -0,0 +1,10 @@ +import Header from '../components/Header' +import Counter from '../components/Counter' + +export default () => ( +
+
+

This is the about page.

+ +
+) diff --git a/examples/with-custom-compress-encodings/pages/index.js b/examples/with-custom-compress-encodings/pages/index.js new file mode 100644 index 0000000000000..c7ae8bffc826d --- /dev/null +++ b/examples/with-custom-compress-encodings/pages/index.js @@ -0,0 +1,10 @@ +import Header from '../components/Header' +import Counter from '../components/Counter' + +export default () => ( +
+
+

HOME PAGE is here!

+ +
+) diff --git a/server/index.js b/server/index.js index 4a46524b947f7..6ba55de0934e4 100644 --- a/server/index.js +++ b/server/index.js @@ -26,10 +26,10 @@ export default class Server { this.hotReloader = dev ? new HotReloader(this.dir, { quiet }) : null this.http = null this.config = getConfig(this.dir) - this.compressTypes = Object.keys(this.config.compress || { gzip: true }) + this.supportedEncodings = Object.keys(this.config.compress || { gzip: true }) this.renderOpts = { dir: this.dir, - compressTypes: this.compressTypes, + supportedEncodings: this.supportedEncodings, dev, staticMarkup } @@ -68,12 +68,12 @@ export default class Server { this.router.get('/_next/main.js', async (req, res, params) => { const p = join(this.dir, '.next/main.js') - await serveStaticWithCompression(req, res, p, this.compressTypes) + await serveStaticWithCompression(req, res, p, this.supportedEncodings) }) this.router.get('/_next/commons.js', async (req, res, params) => { const p = join(this.dir, '.next/commons.js') - await serveStaticWithCompression(req, res, p, this.compressTypes) + await serveStaticWithCompression(req, res, p, this.supportedEncodings) }) this.router.get('/_next/pages/:path*', async (req, res, params) => { @@ -221,7 +221,7 @@ export default class Server { async serveStaticWithCompression (req, res, path) { this._serveStatic(req, res, () => { - return serveStaticWithCompression(req, res, path, this.compressTypes) + return serveStaticWithCompression(req, res, path, this.supportedEncodings) }) } diff --git a/server/render.js b/server/render.js index 87698a0d5518f..82b800e5dcf1c 100644 --- a/server/render.js +++ b/server/render.js @@ -96,9 +96,9 @@ async function doRender (req, res, pathname, query, { return '' + renderToStaticMarkup(doc) } -export async function renderJSON (req, res, page, { dir = process.cwd(), compressTypes } = {}) { +export async function renderJSON (req, res, page, { dir = process.cwd(), supportedEncodings } = {}) { const pagePath = await resolvePath(join(dir, '.next', 'bundles', 'pages', page)) - return serveStaticWithCompression(req, res, pagePath, compressTypes) + return serveStaticWithCompression(req, res, pagePath, supportedEncodings) } export async function renderErrorJSON (err, req, res, { dir = process.cwd(), dev = false } = {}) { @@ -141,8 +141,10 @@ function errorToJSON (err) { return json } -export async function serveStaticWithCompression (req, res, path, compressTypes) { - const encoding = accepts(req).encodings(compressTypes) +export async function serveStaticWithCompression (req, res, path, supportedEncodings) { + const acceptingEncodings = accepts(req).encodings() + const encoding = supportedEncodings.find((e) => acceptingEncodings.indexOf(e) >= 0) + if (!encoding) { return serveStatic(req, res, path) } From 3f9327342bb52e5a003bd1e1510b7dfeb8a4e1ac Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Fri, 6 Jan 2017 11:41:25 +0530 Subject: [PATCH 5/8] Change the compress mappings to return just a stream. --- README.md | 21 ++++++++++--------- .../next.config.js | 17 ++++++++------- server/build/gzip.js | 17 ++++++++++++--- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index dbe5dc3a3d616..bd0dc48cd5303 100644 --- a/README.md +++ b/README.md @@ -524,7 +524,6 @@ Here's an example `.babelrc` file: ### Customizing compression encodings -

Examples @@ -533,29 +532,31 @@ Here's an example `.babelrc` file: By default Next.js will compress your core JavaScript assets for `gzip` encoding while it builds(with `next build`) the app. If you need to add support for more encoding, you could simply add it via `next.config.js`. -Here's an example config which adds supports for both `br` and `gzip` encodings. +Here's an example config which adds supports for both [`br`](https://en.wikipedia.org/wiki/Brotli) and `gzip` encodings. ```js -var fs = require('fs') var zlib = require('zlib') var iltorb = require('iltorb') module.exports = { - // Return a stream of compressed file for each of the encodings you want - // + // Returns a map of compression streams for the types of encodings you want to support + // Here's a list of common encoding types: https://goo.gl/ke7zOK + // The first listed encoding has the higher priority over others. // In this case, it'll try to serve the `br` version if the browser supports it. // Otherwise, it'll server gzipped version. compress: { - br: function(filePath) { - return fs.createReadStream(filePath).pipe(iltorb.compressStream()) + br: function () { + return iltorb.compressStream() }, - gzip: function(filePath) { - return fs.createReadStream(filePath).pipe(zlib.createGzip()) + gzip: function () { + // You can also return a promise which resolve a compression stream + return new Promise(function (resolve) { + resolve(zlib.createGzip()) + }) } } } - ``` ## Production deployment diff --git a/examples/with-custom-compress-encodings/next.config.js b/examples/with-custom-compress-encodings/next.config.js index 4127922955aa3..8f15ec78bee90 100644 --- a/examples/with-custom-compress-encodings/next.config.js +++ b/examples/with-custom-compress-encodings/next.config.js @@ -1,19 +1,22 @@ -var fs = require('fs') var zlib = require('zlib') var iltorb = require('iltorb') module.exports = { - // Return a stream of compressed file for each of the encodings you want - // + // Returns a map of compression streams for the types of encodings you want to support + // Here's a list of common encoding types: https://goo.gl/ke7zOK + // The first listed encoding has the higher priority over others. // In this case, it'll try to serve the `br` version if the browser supports it. // Otherwise, it'll server gzipped version. compress: { - br: function (filePath) { - return fs.createReadStream(filePath).pipe(iltorb.compressStream()) + br: function () { + return iltorb.compressStream() }, - gzip: function (filePath) { - return fs.createReadStream(filePath).pipe(zlib.createGzip()) + gzip: function () { + // You can also return a promise which resolve a compression stream + return new Promise(function (resolve) { + resolve(zlib.createGzip()) + }) } } } diff --git a/server/build/gzip.js b/server/build/gzip.js index 708345e1e4745..4807cf321e4f2 100644 --- a/server/build/gzip.js +++ b/server/build/gzip.js @@ -30,13 +30,24 @@ export default async function gzipAssets (dir) { export function compress (config, filePath) { const compressionMap = config.compress || { - gzip: (f) => fs.createReadStream(f).pipe(zlib.createGzip()) + gzip: () => zlib.createGzip() } const promises = Object.keys(compressionMap).map((type) => { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { + const input = fs.createReadStream(filePath) const output = fs.createWriteStream(`${filePath}.${type}`) - const stream = compressionMap[type](filePath).pipe(output) + // We accept stream resolved via a promise or not + const compression = await compressionMap[type](filePath) + + // We need to handle errors like this. + // See: http://stackoverflow.com/a/22389498 + input.on('error', reject) + compression.on('error', reject) + + const stream = input.pipe(compression).pipe(output) + + // Handle the final stream stream.on('error', reject) stream.on('finish', resolve) }) From 83c579846205d2a2f717411b767e837fe724fb96 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Fri, 6 Jan 2017 11:46:16 +0530 Subject: [PATCH 6/8] Change gzip wording into compress over the code. --- server/build/{gzip.js => compress.js} | 4 ++-- server/build/index.js | 4 ++-- server/render.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) rename server/build/{gzip.js => compress.js} (93%) diff --git a/server/build/gzip.js b/server/build/compress.js similarity index 93% rename from server/build/gzip.js rename to server/build/compress.js index 4807cf321e4f2..5ded3cb7ad802 100644 --- a/server/build/gzip.js +++ b/server/build/compress.js @@ -4,7 +4,7 @@ import zlib from 'zlib' import glob from 'glob-promise' import getConfig from '../config' -export default async function gzipAssets (dir) { +export default async function compressAssets (dir) { const nextDir = path.resolve(dir, '.next') const config = getConfig(dir) @@ -20,7 +20,7 @@ export default async function gzipAssets (dir) { ] while (true) { - // gzip only 10 assets in parallel at a time. + // compress only 10 assets in parallel at a time. const currentChunk = allAssets.splice(0, 10) if (currentChunk.length === 0) break diff --git a/server/build/index.js b/server/build/index.js index e4fcb84574c05..56015e3629216 100644 --- a/server/build/index.js +++ b/server/build/index.js @@ -1,6 +1,6 @@ import webpack from './webpack' import clean from './clean' -import gzipAssets from './gzip' +import compressAssets from './compress' export default async function build (dir) { const [compiler] = await Promise.all([ @@ -9,7 +9,7 @@ export default async function build (dir) { ]) await runCompiler(compiler) - await gzipAssets(dir) + await compressAssets(dir) } function runCompiler (compiler) { diff --git a/server/render.js b/server/render.js index 82b800e5dcf1c..69bd832f7c556 100644 --- a/server/render.js +++ b/server/render.js @@ -150,9 +150,9 @@ export async function serveStaticWithCompression (req, res, path, supportedEncod } try { - const gzipPath = `${path}.${encoding}` + const compressedPath = `${path}.${encoding}` res.setHeader('Content-Encoding', encoding) - await serveStatic(req, res, gzipPath) + await serveStatic(req, res, compressedPath) } catch (ex) { if (ex.code === 'ENOENT') { res.removeHeader('Content-Encoding') From fcabbc8e1ffa5c5a470ae0971f4bc89a23e200c8 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Fri, 6 Jan 2017 11:48:11 +0530 Subject: [PATCH 7/8] Fix a typo. --- examples/with-custom-compress-encodings/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/with-custom-compress-encodings/package.json b/examples/with-custom-compress-encodings/package.json index 9f649e1a9889c..0053ca9293d50 100644 --- a/examples/with-custom-compress-encodings/package.json +++ b/examples/with-custom-compress-encodings/package.json @@ -1,5 +1,5 @@ { - "name": "shared-modules", + "name": "with-custom-compress-encodings", "version": "1.0.0", "description": "This example features:", "main": "index.js", From 318a9d5c93bd7f7a6c4720e869ededb33e21cc5e Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Fri, 6 Jan 2017 11:58:03 +0530 Subject: [PATCH 8/8] Fix some typo. --- README.md | 4 ++-- examples/with-custom-compress-encodings/next.config.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bd0dc48cd5303..1da05ae8c503c 100644 --- a/README.md +++ b/README.md @@ -543,8 +543,8 @@ module.exports = { // Here's a list of common encoding types: https://goo.gl/ke7zOK // The first listed encoding has the higher priority over others. - // In this case, it'll try to serve the `br` version if the browser supports it. - // Otherwise, it'll server gzipped version. + // In this case, first it'll try to serve the `br` version if the browser supports it. + // Otherwise, it'll serve the gzipped version. compress: { br: function () { return iltorb.compressStream() diff --git a/examples/with-custom-compress-encodings/next.config.js b/examples/with-custom-compress-encodings/next.config.js index 8f15ec78bee90..110c0f94b0347 100644 --- a/examples/with-custom-compress-encodings/next.config.js +++ b/examples/with-custom-compress-encodings/next.config.js @@ -6,8 +6,8 @@ module.exports = { // Here's a list of common encoding types: https://goo.gl/ke7zOK // The first listed encoding has the higher priority over others. - // In this case, it'll try to serve the `br` version if the browser supports it. - // Otherwise, it'll server gzipped version. + // In this case, first it'll try to serve the `br` version if the browser supports it. + // Otherwise, it'll serve the gzipped version. compress: { br: function () { return iltorb.compressStream()