mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Remove pre-gzip support (#1155)
This commit is contained in:
parent
540a86f007
commit
fca186f9e7
|
@ -42,7 +42,6 @@
|
|||
]
|
||||
},
|
||||
"dependencies": {
|
||||
"accepts": "1.3.3",
|
||||
"ansi-html": "0.0.7",
|
||||
"babel-core": "6.23.1",
|
||||
"babel-generator": "6.22.0",
|
||||
|
@ -69,7 +68,6 @@
|
|||
"is-windows-bash": "1.0.3",
|
||||
"json-loader": "0.5.4",
|
||||
"loader-utils": "0.2.16",
|
||||
"mime-types": "2.1.14",
|
||||
"minimist": "1.2.0",
|
||||
"mkdirp-then": "1.2.0",
|
||||
"mz": "2.6.0",
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import zlib from 'zlib'
|
||||
import glob from 'glob-promise'
|
||||
|
||||
export default async function gzipAssets (dir) {
|
||||
const nextDir = path.resolve(dir, '.next')
|
||||
|
||||
const coreAssets = [
|
||||
path.join(nextDir, 'commons.js'),
|
||||
path.join(nextDir, 'main.js')
|
||||
]
|
||||
const pages = await glob('bundles/pages/**/*.json', { cwd: nextDir })
|
||||
|
||||
const allAssets = [
|
||||
...coreAssets,
|
||||
...pages.map(page => path.join(nextDir, page))
|
||||
]
|
||||
|
||||
while (true) {
|
||||
// gzip only 10 assets in parallel at a time.
|
||||
const currentChunk = allAssets.splice(0, 10)
|
||||
if (currentChunk.length === 0) break
|
||||
|
||||
await Promise.all(currentChunk.map(gzip))
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
|
@ -4,7 +4,6 @@ import fs from 'mz/fs'
|
|||
import uuid from 'uuid'
|
||||
import del from 'del'
|
||||
import webpack from './webpack'
|
||||
import gzipAssets from './gzip'
|
||||
import replaceCurrentBuild from './replace'
|
||||
|
||||
export default async function build (dir) {
|
||||
|
@ -13,7 +12,6 @@ export default async function build (dir) {
|
|||
|
||||
try {
|
||||
await runCompiler(compiler)
|
||||
await gzipAssets(buildDir)
|
||||
await writeBuildId(buildDir)
|
||||
} catch (err) {
|
||||
console.error(`> Failed to build on ${buildDir}`)
|
||||
|
|
|
@ -8,8 +8,7 @@ import {
|
|||
renderJSON,
|
||||
renderErrorJSON,
|
||||
sendHTML,
|
||||
serveStatic,
|
||||
serveStaticWithGzip
|
||||
serveStatic
|
||||
} from './render'
|
||||
import Router from './router'
|
||||
import HotReloader from './hot-reloader'
|
||||
|
@ -84,13 +83,13 @@ export default class Server {
|
|||
'/_next/:buildId/main.js': async (req, res, params) => {
|
||||
this.handleBuildId(params.buildId, res)
|
||||
const p = join(this.dir, '.next/main.js')
|
||||
await this.serveStaticWithGzip(req, res, p)
|
||||
await this.serveStatic(req, res, p)
|
||||
},
|
||||
|
||||
'/_next/:buildId/commons.js': async (req, res, params) => {
|
||||
this.handleBuildId(params.buildId, res)
|
||||
const p = join(this.dir, '.next/commons.js')
|
||||
await this.serveStaticWithGzip(req, res, p)
|
||||
await this.serveStatic(req, res, p)
|
||||
},
|
||||
|
||||
'/_next/:buildId/pages/:path*': async (req, res, params) => {
|
||||
|
@ -251,12 +250,6 @@ export default class Server {
|
|||
return renderErrorJSON(err, req, res, this.renderOpts)
|
||||
}
|
||||
|
||||
async serveStaticWithGzip (req, res, path) {
|
||||
await this._serveStatic(req, res, () => {
|
||||
return serveStaticWithGzip(req, res, path)
|
||||
})
|
||||
}
|
||||
|
||||
serveStatic (req, res, path) {
|
||||
this._serveStatic(req, res, () => {
|
||||
return serveStatic(req, res, path)
|
||||
|
|
|
@ -2,9 +2,6 @@ import { join } from 'path'
|
|||
import { createElement } from 'react'
|
||||
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
|
||||
import send from 'send'
|
||||
import fs from 'mz/fs'
|
||||
import accepts from 'accepts'
|
||||
import mime from 'mime-types'
|
||||
import requireModule from './require'
|
||||
import resolvePath from './resolve'
|
||||
import readPage from './read-page'
|
||||
|
@ -105,7 +102,7 @@ async function doRender (req, res, pathname, query, {
|
|||
|
||||
export async function renderJSON (req, res, page, { dir = process.cwd() } = {}) {
|
||||
const pagePath = await resolvePath(join(dir, '.next', 'bundles', 'pages', page))
|
||||
return serveStaticWithGzip(req, res, pagePath)
|
||||
return serveStatic(req, res, pagePath)
|
||||
}
|
||||
|
||||
export async function renderErrorJSON (err, req, res, { dir = process.cwd(), dev = false } = {}) {
|
||||
|
@ -147,38 +144,6 @@ function errorToJSON (err) {
|
|||
return json
|
||||
}
|
||||
|
||||
export async function serveStaticWithGzip (req, res, path) {
|
||||
const encoding = accepts(req).encodings(['gzip'])
|
||||
if (encoding !== 'gzip') {
|
||||
return serveStatic(req, res, path)
|
||||
}
|
||||
|
||||
const gzipPath = `${path}.gz`
|
||||
|
||||
try {
|
||||
// We need to check the existance of the gzipPath.
|
||||
// Getting `ENOENT` error from the `serveStatic` is inconsistent and
|
||||
// didn't work on all the cases.
|
||||
//
|
||||
// And this won't give us a race condition because we know that
|
||||
// we don't add gzipped files at runtime.
|
||||
await fs.stat(gzipPath)
|
||||
} catch (ex) {
|
||||
// Handles the error thrown by fs.stat
|
||||
if (ex.code === 'ENOENT') {
|
||||
// Seems like there's no gzipped file. Let's serve the uncompressed file.
|
||||
return serveStatic(req, res, path)
|
||||
}
|
||||
|
||||
throw ex
|
||||
}
|
||||
|
||||
const contentType = mime.lookup(path) || 'application/octet-stream'
|
||||
res.setHeader('Content-Type', contentType)
|
||||
res.setHeader('Content-Encoding', 'gzip')
|
||||
return serveStatic(req, res, gzipPath)
|
||||
}
|
||||
|
||||
export function serveStatic (req, res, path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
send(req, path)
|
||||
|
|
|
@ -38,17 +38,6 @@ describe('Production Usage', () => {
|
|||
})
|
||||
|
||||
describe('JSON pages', () => {
|
||||
describe('when asked for a gzipped page', () => {
|
||||
it('should serve the gzipped page', async () => {
|
||||
const url = `http://localhost:${appPort}/_next/${app.renderOpts.buildId}/pages`
|
||||
const res = await fetch(url, { compress: true })
|
||||
expect(res.headers.get('Content-Encoding')).toBe('gzip')
|
||||
|
||||
const page = await res.json()
|
||||
expect(page.component).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('when asked for a normal page', () => {
|
||||
it('should serve the normal page', async () => {
|
||||
const url = `http://localhost:${appPort}/_next/${app.renderOpts.buildId}/pages`
|
||||
|
|
Loading…
Reference in a new issue