From e3d68ff401fc21f99757bde29355c577dab2847d Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Wed, 5 Apr 2017 02:05:25 +0530 Subject: [PATCH] Fix a typo and remove unwanted code. --- server/index.js | 52 ++++++++---------------------------------------- server/render.js | 38 +++++++++++------------------------ 2 files changed, 20 insertions(+), 70 deletions(-) diff --git a/server/index.js b/server/index.js index c45712fa..16a4b7f4 100644 --- a/server/index.js +++ b/server/index.js @@ -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') diff --git a/server/render.js b/server/render.js index 1c202e43..f7ee665e 100644 --- a/server/render.js +++ b/server/render.js @@ -112,30 +112,14 @@ async function doRender (req, res, pathname, query, { return '' + 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