mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
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.
This commit is contained in:
parent
84fc6781bb
commit
29c226771c
23
server/build/gzip.js
Normal file
23
server/build/gzip.js
Normal file
|
@ -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)
|
||||||
|
})
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
import webpack from './webpack'
|
import webpack from './webpack'
|
||||||
import clean from './clean'
|
import clean from './clean'
|
||||||
|
import gzipAssets from './gzip'
|
||||||
|
|
||||||
export default async function build (dir) {
|
export default async function build (dir) {
|
||||||
const [compiler] = await Promise.all([
|
const [compiler] = await Promise.all([
|
||||||
|
@ -8,6 +9,7 @@ export default async function build (dir) {
|
||||||
])
|
])
|
||||||
|
|
||||||
await runCompiler(compiler)
|
await runCompiler(compiler)
|
||||||
|
await gzipAssets(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
function runCompiler (compiler) {
|
function runCompiler (compiler) {
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { resolve, join } from 'path'
|
import { resolve, join } from 'path'
|
||||||
import { parse } from 'url'
|
import { parse } from 'url'
|
||||||
import http from 'http'
|
import http from 'http'
|
||||||
|
import fs from 'mz/fs'
|
||||||
import send from 'send'
|
import send from 'send'
|
||||||
|
import accepts from 'accepts'
|
||||||
import {
|
import {
|
||||||
renderToHTML,
|
renderToHTML,
|
||||||
renderErrorToHTML,
|
renderErrorToHTML,
|
||||||
|
@ -61,12 +63,12 @@ export default class Server {
|
||||||
|
|
||||||
this.router.get('/_next/main.js', async (req, res, params) => {
|
this.router.get('/_next/main.js', async (req, res, params) => {
|
||||||
const p = join(this.dir, '.next/main.js')
|
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) => {
|
this.router.get('/_next/commons.js', async (req, res, params) => {
|
||||||
const p = join(this.dir, '.next/commons.js')
|
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) => {
|
this.router.get('/_next/pages/:path*', async (req, res, params) => {
|
||||||
|
@ -212,6 +214,22 @@ export default class Server {
|
||||||
return renderErrorJSON(err, res, this.renderOpts)
|
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) {
|
serveStatic (req, res, path) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
send(req, path)
|
send(req, path)
|
||||||
|
|
Loading…
Reference in a new issue