提交 29c22677 编写于 作者: A Arunoda Susiripala 提交者: Guillermo Rauch

Add AOT gzip content-encoding support for main build files. (#565)

* 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.
上级 84fc6781
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)
})
}
import webpack from './webpack'
import clean from './clean'
import gzipAssets from './gzip'
export default async function build (dir) {
const [compiler] = await Promise.all([
......@@ -8,6 +9,7 @@ export default async function build (dir) {
])
await runCompiler(compiler)
await gzipAssets(dir)
}
function runCompiler (compiler) {
......
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,
......@@ -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) => {
......@@ -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)
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册