1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Fix a typo and remove unwanted code.

This commit is contained in:
Arunoda Susiripala 2017-04-05 02:05:25 +05:30
parent c95d2b28d0
commit e3d68ff401
2 changed files with 20 additions and 70 deletions

View file

@ -6,8 +6,6 @@ import http, { STATUS_CODES } from 'http'
import {
renderToHTML,
renderErrorToHTML,
renderJSON,
renderErrorJSON,
sendHTML,
serveStatic,
renderScript,
@ -124,16 +122,20 @@ export default class Server {
const error = new Error('INVALID_BUILD_ID')
const customFields = { buildIdMismatched: true }
await renderScriptError(req, res, page, error, customFields, this.renderOpts)
return
return await renderScriptError(req, res, page, error, customFields, this.renderOpts)
}
if (this.dev) {
try {
await this.hotReloader.ensurePage(page)
} catch (error) {
return await renderScriptError(req, res, page, error, {}, this.renderOpts)
}
const compilationErr = this.getCompilationError(page)
if (compilationErr) {
const customFields = { buildError: true }
await renderScriptError(req, res, page, compilationErr, customFields, this.renderOpts)
return
return await renderScriptError(req, res, page, compilationErr, customFields, this.renderOpts)
}
}
@ -257,40 +259,6 @@ export default class Server {
return this.renderError(null, req, res, pathname, query)
}
async renderJSON (req, res, page) {
if (this.dev) {
const compilationErr = this.getCompilationError(page)
if (compilationErr) {
return this.renderErrorJSON(compilationErr, req, res)
}
}
try {
return await renderJSON(req, res, page, this.renderOpts)
} catch (err) {
if (err.code === 'ENOENT') {
res.statusCode = 404
return this.renderErrorJSON(null, req, res)
} else {
if (!this.quiet) console.error(err)
res.statusCode = 500
return this.renderErrorJSON(err, req, res)
}
}
}
async renderErrorJSON (err, req, res) {
if (this.dev) {
const compilationErr = this.getCompilationError('/_error')
if (compilationErr) {
res.statusCode = 500
return renderErrorJSON(compilationErr, req, res, this.renderOpts)
}
}
return renderErrorJSON(err, req, res, this.renderOpts)
}
async serveStatic (req, res, path) {
try {
return await serveStatic(req, res, path)
@ -303,10 +271,6 @@ export default class Server {
}
}
serveScript (req, res, path) {
return serveStatic(req, res, path)
}
readBuildId () {
const buildIdPath = join(this.dir, '.next', 'BUILD_ID')
const buildId = fs.readFileSync(buildIdPath, 'utf8')

View file

@ -112,30 +112,14 @@ async function doRender (req, res, pathname, query, {
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
}
export async function renderJSON (req, res, page, { dir = process.cwd(), hotReloader } = {}) {
await ensurePage(page, { dir, hotReloader })
const pagePath = await resolvePath(join(dir, '.next', 'bundles', 'pages', page))
return serveStatic(req, res, pagePath)
}
export async function renderScript (req, res, page, opts) {
try {
if (opts.dev) {
await opts.hotReloader.ensurePage(page)
}
const path = join(opts.dir, '.next', 'client-bundles', 'pages', page)
const realPath = await resolvePath(path)
await serveStatic(req, res, realPath)
} catch (err) {
if (err.code === 'ENOENT') {
res.setHeader('Content-Type', 'text/javascript')
res.end(`
var error = new Error('Page not exists: ${page}')
error.pageNotFound = true
error.statusCode = 404
NEXT_PAGE_LOADER.registerPage('${page}', error)
`)
renderScriptError(req, res, page, err, {}, opts)
return
}
@ -144,6 +128,17 @@ export async function renderScript (req, res, page, opts) {
}
export async function renderScriptError (req, res, page, error, customFields, opts) {
if (error.code === 'ENOENT') {
res.setHeader('Content-Type', 'text/javascript')
res.end(`
var error = new Error('Page not exists: ${page}')
error.pageNotFound = true
error.statusCode = 404
NEXT_PAGE_LOADER.registerPage('${page}', error)
`)
return
}
res.setHeader('Content-Type', 'text/javascript')
const errorJson = {
...errorToJSON(error),
@ -156,15 +151,6 @@ export async function renderScriptError (req, res, page, error, customFields, op
`)
}
export async function renderErrorJSON (err, req, res, { dir = process.cwd(), dev = false } = {}) {
const component = await readPage(join(dir, '.next', 'bundles', 'pages', '_error'))
sendJSON(res, {
component,
err: err && dev ? errorToJSON(err) : null
}, req.method)
}
export function sendHTML (res, html, method) {
if (res.finished) return