1
0
Fork 0
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:
Tim Neutkens 2017-02-15 15:49:45 +01:00 committed by GitHub
parent 540a86f007
commit fca186f9e7
7 changed files with 231 additions and 389 deletions

View file

@ -42,7 +42,6 @@
] ]
}, },
"dependencies": { "dependencies": {
"accepts": "1.3.3",
"ansi-html": "0.0.7", "ansi-html": "0.0.7",
"babel-core": "6.23.1", "babel-core": "6.23.1",
"babel-generator": "6.22.0", "babel-generator": "6.22.0",
@ -69,7 +68,6 @@
"is-windows-bash": "1.0.3", "is-windows-bash": "1.0.3",
"json-loader": "0.5.4", "json-loader": "0.5.4",
"loader-utils": "0.2.16", "loader-utils": "0.2.16",
"mime-types": "2.1.14",
"minimist": "1.2.0", "minimist": "1.2.0",
"mkdirp-then": "1.2.0", "mkdirp-then": "1.2.0",
"mz": "2.6.0", "mz": "2.6.0",

View file

@ -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)
})
}

View file

@ -4,7 +4,6 @@ import fs from 'mz/fs'
import uuid from 'uuid' import uuid from 'uuid'
import del from 'del' import del from 'del'
import webpack from './webpack' import webpack from './webpack'
import gzipAssets from './gzip'
import replaceCurrentBuild from './replace' import replaceCurrentBuild from './replace'
export default async function build (dir) { export default async function build (dir) {
@ -13,7 +12,6 @@ export default async function build (dir) {
try { try {
await runCompiler(compiler) await runCompiler(compiler)
await gzipAssets(buildDir)
await writeBuildId(buildDir) await writeBuildId(buildDir)
} catch (err) { } catch (err) {
console.error(`> Failed to build on ${buildDir}`) console.error(`> Failed to build on ${buildDir}`)

View file

@ -8,8 +8,7 @@ import {
renderJSON, renderJSON,
renderErrorJSON, renderErrorJSON,
sendHTML, sendHTML,
serveStatic, serveStatic
serveStaticWithGzip
} from './render' } from './render'
import Router from './router' import Router from './router'
import HotReloader from './hot-reloader' import HotReloader from './hot-reloader'
@ -84,13 +83,13 @@ export default class Server {
'/_next/:buildId/main.js': async (req, res, params) => { '/_next/:buildId/main.js': async (req, res, params) => {
this.handleBuildId(params.buildId, res) this.handleBuildId(params.buildId, res)
const p = join(this.dir, '.next/main.js') 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) => { '/_next/:buildId/commons.js': async (req, res, params) => {
this.handleBuildId(params.buildId, res) this.handleBuildId(params.buildId, res)
const p = join(this.dir, '.next/commons.js') 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) => { '/_next/:buildId/pages/:path*': async (req, res, params) => {
@ -251,12 +250,6 @@ export default class Server {
return renderErrorJSON(err, req, res, this.renderOpts) 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) { serveStatic (req, res, path) {
this._serveStatic(req, res, () => { this._serveStatic(req, res, () => {
return serveStatic(req, res, path) return serveStatic(req, res, path)

View file

@ -2,9 +2,6 @@ import { join } from 'path'
import { createElement } from 'react' import { createElement } from 'react'
import { renderToString, renderToStaticMarkup } from 'react-dom/server' import { renderToString, renderToStaticMarkup } from 'react-dom/server'
import send from 'send' import send from 'send'
import fs from 'mz/fs'
import accepts from 'accepts'
import mime from 'mime-types'
import requireModule from './require' import requireModule from './require'
import resolvePath from './resolve' import resolvePath from './resolve'
import readPage from './read-page' 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() } = {}) { export async function renderJSON (req, res, page, { dir = process.cwd() } = {}) {
const pagePath = await resolvePath(join(dir, '.next', 'bundles', 'pages', page)) 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 } = {}) { export async function renderErrorJSON (err, req, res, { dir = process.cwd(), dev = false } = {}) {
@ -147,38 +144,6 @@ function errorToJSON (err) {
return json 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) { export function serveStatic (req, res, path) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
send(req, path) send(req, path)

View file

@ -38,17 +38,6 @@ describe('Production Usage', () => {
}) })
describe('JSON pages', () => { 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', () => { describe('when asked for a normal page', () => {
it('should serve the normal page', async () => { it('should serve the normal page', async () => {
const url = `http://localhost:${appPort}/_next/${app.renderOpts.buildId}/pages` const url = `http://localhost:${appPort}/_next/${app.renderOpts.buildId}/pages`

517
yarn.lock

File diff suppressed because it is too large Load diff