Skip to content

Commit

Permalink
Add AOT gzip content-encoding support for main build files. (#565)
Browse files Browse the repository at this point in the history
* Add AOT gzip content-encoding support.
Currently we only do this for
main.js and commons.js only.

* Remove unwanted await.

* Use Promise.all to gzip assets in parallel.
  • Loading branch information
arunoda authored and rauchg committed Dec 29, 2016
1 parent 84fc678 commit 29c2267
Show file tree
Hide file tree
Showing 4 changed files with 583 additions and 241 deletions.
23 changes: 23 additions & 0 deletions server/build/gzip.js
@@ -0,0 +1,23 @@
import fs from 'fs'
import path from 'path'
import zlib from 'zlib'

export default async function gzipAssets (dir) {
const nextDir = path.resolve(dir, '.next')

await Promise.all([
gzip(path.resolve(nextDir, 'commons.js')),
gzip(path.resolve(nextDir, 'main.js'))
])
}

export function gzip (filePath) {
const input = fs.createReadStream(filePath)
const output = fs.createWriteStream(`${filePath}.gz`)

return new Promise((resolve, reject) => {
const stream = input.pipe(zlib.createGzip()).pipe(output)
stream.on('error', reject)
stream.on('finish', resolve)
})
}
2 changes: 2 additions & 0 deletions server/build/index.js
@@ -1,5 +1,6 @@
import webpack from './webpack'
import clean from './clean'
import gzipAssets from './gzip'

export default async function build (dir) {
const [compiler] = await Promise.all([
Expand All @@ -8,6 +9,7 @@ export default async function build (dir) {
])

await runCompiler(compiler)
await gzipAssets(dir)
}

function runCompiler (compiler) {
Expand Down
22 changes: 20 additions & 2 deletions server/index.js
@@ -1,7 +1,9 @@
import { resolve, join } from 'path'
import { parse } from 'url'
import http from 'http'
import fs from 'mz/fs'
import send from 'send'
import accepts from 'accepts'
import {
renderToHTML,
renderErrorToHTML,
Expand Down Expand Up @@ -61,12 +63,12 @@ export default class Server {

this.router.get('/_next/main.js', async (req, res, params) => {
const p = join(this.dir, '.next/main.js')
await this.serveStatic(req, res, p)
await this.serveStaticWithGzip(req, res, p)
})

this.router.get('/_next/commons.js', async (req, res, params) => {
const p = join(this.dir, '.next/commons.js')
await this.serveStatic(req, res, p)
await this.serveStaticWithGzip(req, res, p)
})

this.router.get('/_next/pages/:path*', async (req, res, params) => {
Expand Down Expand Up @@ -212,6 +214,22 @@ export default class Server {
return renderErrorJSON(err, res, this.renderOpts)
}

async serveStaticWithGzip (req, res, path) {
const encoding = accepts(req).encodings(['gzip'])
if (encoding !== 'gzip') {
return this.serveStatic(req, res, path)
}

const gzipPath = `${path}.gz`
const exists = await fs.exists(gzipPath)
if (!exists) {
return this.serveStatic(req, res, path)
}

res.setHeader('Content-Encoding', 'gzip')
return this.serveStatic(req, res, gzipPath)
}

serveStatic (req, res, path) {
return new Promise((resolve, reject) => {
send(req, path)
Expand Down

0 comments on commit 29c2267

Please sign in to comment.