Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3d68ff

Browse files
committed
Fix a typo and remove unwanted code.
1 parent c95d2b2 commit e3d68ff

File tree

2 files changed

+20
-70
lines changed

2 files changed

+20
-70
lines changed

server/index.js

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import http, { STATUS_CODES } from 'http'
66
import {
77
renderToHTML,
88
renderErrorToHTML,
9-
renderJSON,
10-
renderErrorJSON,
119
sendHTML,
1210
serveStatic,
1311
renderScript,
@@ -124,16 +122,20 @@ export default class Server {
124122
const error = new Error('INVALID_BUILD_ID')
125123
const customFields = { buildIdMismatched: true }
126124

127-
await renderScriptError(req, res, page, error, customFields, this.renderOpts)
128-
return
125+
return await renderScriptError(req, res, page, error, customFields, this.renderOpts)
129126
}
130127

131128
if (this.dev) {
129+
try {
130+
await this.hotReloader.ensurePage(page)
131+
} catch (error) {
132+
return await renderScriptError(req, res, page, error, {}, this.renderOpts)
133+
}
134+
132135
const compilationErr = this.getCompilationError(page)
133136
if (compilationErr) {
134137
const customFields = { buildError: true }
135-
await renderScriptError(req, res, page, compilationErr, customFields, this.renderOpts)
136-
return
138+
return await renderScriptError(req, res, page, compilationErr, customFields, this.renderOpts)
137139
}
138140
}
139141

@@ -257,40 +259,6 @@ export default class Server {
257259
return this.renderError(null, req, res, pathname, query)
258260
}
259261

260-
async renderJSON (req, res, page) {
261-
if (this.dev) {
262-
const compilationErr = this.getCompilationError(page)
263-
if (compilationErr) {
264-
return this.renderErrorJSON(compilationErr, req, res)
265-
}
266-
}
267-
268-
try {
269-
return await renderJSON(req, res, page, this.renderOpts)
270-
} catch (err) {
271-
if (err.code === 'ENOENT') {
272-
res.statusCode = 404
273-
return this.renderErrorJSON(null, req, res)
274-
} else {
275-
if (!this.quiet) console.error(err)
276-
res.statusCode = 500
277-
return this.renderErrorJSON(err, req, res)
278-
}
279-
}
280-
}
281-
282-
async renderErrorJSON (err, req, res) {
283-
if (this.dev) {
284-
const compilationErr = this.getCompilationError('/_error')
285-
if (compilationErr) {
286-
res.statusCode = 500
287-
return renderErrorJSON(compilationErr, req, res, this.renderOpts)
288-
}
289-
}
290-
291-
return renderErrorJSON(err, req, res, this.renderOpts)
292-
}
293-
294262
async serveStatic (req, res, path) {
295263
try {
296264
return await serveStatic(req, res, path)
@@ -303,10 +271,6 @@ export default class Server {
303271
}
304272
}
305273

306-
serveScript (req, res, path) {
307-
return serveStatic(req, res, path)
308-
}
309-
310274
readBuildId () {
311275
const buildIdPath = join(this.dir, '.next', 'BUILD_ID')
312276
const buildId = fs.readFileSync(buildIdPath, 'utf8')

server/render.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,30 +112,14 @@ async function doRender (req, res, pathname, query, {
112112
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
113113
}
114114

115-
export async function renderJSON (req, res, page, { dir = process.cwd(), hotReloader } = {}) {
116-
await ensurePage(page, { dir, hotReloader })
117-
const pagePath = await resolvePath(join(dir, '.next', 'bundles', 'pages', page))
118-
return serveStatic(req, res, pagePath)
119-
}
120-
121115
export async function renderScript (req, res, page, opts) {
122116
try {
123-
if (opts.dev) {
124-
await opts.hotReloader.ensurePage(page)
125-
}
126-
127117
const path = join(opts.dir, '.next', 'client-bundles', 'pages', page)
128118
const realPath = await resolvePath(path)
129119
await serveStatic(req, res, realPath)
130120
} catch (err) {
131121
if (err.code === 'ENOENT') {
132-
res.setHeader('Content-Type', 'text/javascript')
133-
res.end(`
134-
var error = new Error('Page not exists: ${page}')
135-
error.pageNotFound = true
136-
error.statusCode = 404
137-
NEXT_PAGE_LOADER.registerPage('${page}', error)
138-
`)
122+
renderScriptError(req, res, page, err, {}, opts)
139123
return
140124
}
141125

@@ -144,6 +128,17 @@ export async function renderScript (req, res, page, opts) {
144128
}
145129

146130
export async function renderScriptError (req, res, page, error, customFields, opts) {
131+
if (error.code === 'ENOENT') {
132+
res.setHeader('Content-Type', 'text/javascript')
133+
res.end(`
134+
var error = new Error('Page not exists: ${page}')
135+
error.pageNotFound = true
136+
error.statusCode = 404
137+
NEXT_PAGE_LOADER.registerPage('${page}', error)
138+
`)
139+
return
140+
}
141+
147142
res.setHeader('Content-Type', 'text/javascript')
148143
const errorJson = {
149144
...errorToJSON(error),
@@ -156,15 +151,6 @@ export async function renderScriptError (req, res, page, error, customFields, op
156151
`)
157152
}
158153

159-
export async function renderErrorJSON (err, req, res, { dir = process.cwd(), dev = false } = {}) {
160-
const component = await readPage(join(dir, '.next', 'bundles', 'pages', '_error'))
161-
162-
sendJSON(res, {
163-
component,
164-
err: err && dev ? errorToJSON(err) : null
165-
}, req.method)
166-
}
167-
168154
export function sendHTML (res, html, method) {
169155
if (res.finished) return
170156

0 commit comments

Comments
 (0)