2016-10-19 12:58:08 +00:00
|
|
|
import { join } from 'path'
|
2016-10-05 23:52:50 +00:00
|
|
|
import { createElement } from 'react'
|
|
|
|
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
|
2016-12-31 12:46:23 +00:00
|
|
|
import send from 'send'
|
2017-04-18 15:45:42 +00:00
|
|
|
import generateETag from 'etag'
|
|
|
|
import fresh from 'fresh'
|
2016-10-15 19:49:42 +00:00
|
|
|
import requireModule from './require'
|
2017-04-06 10:09:26 +00:00
|
|
|
import getConfig from './config'
|
2017-01-01 10:07:56 +00:00
|
|
|
import resolvePath from './resolve'
|
2016-12-20 17:27:43 +00:00
|
|
|
import { Router } from '../lib/router'
|
2017-01-20 19:33:46 +00:00
|
|
|
import { loadGetInitialProps } from '../lib/utils'
|
2016-12-16 18:42:40 +00:00
|
|
|
import Head, { defaultHead } from '../lib/head'
|
2016-10-05 23:52:50 +00:00
|
|
|
import App from '../lib/app'
|
2017-04-01 21:03:40 +00:00
|
|
|
import ErrorDebug from '../lib/error-debug'
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
export async function render (req, res, pathname, query, opts) {
|
|
|
|
const html = await renderToHTML(req, res, pathname, opts)
|
2017-05-25 16:28:08 +00:00
|
|
|
sendHTML(req, res, html, req.method, opts)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function renderToHTML (req, res, pathname, query, opts) {
|
|
|
|
return doRender(req, res, pathname, query, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function renderError (err, req, res, pathname, query, opts) {
|
|
|
|
const html = await renderErrorToHTML(err, req, res, query, opts)
|
2017-05-25 16:28:08 +00:00
|
|
|
sendHTML(req, res, html, req.method, opts)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function renderErrorToHTML (err, req, res, pathname, query, opts = {}) {
|
2017-01-12 01:58:20 +00:00
|
|
|
return doRender(req, res, pathname, query, { ...opts, err, page: '_error' })
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function doRender (req, res, pathname, query, {
|
|
|
|
err,
|
|
|
|
page,
|
2017-01-11 20:16:18 +00:00
|
|
|
buildId,
|
2017-03-07 18:43:56 +00:00
|
|
|
buildStats,
|
2017-02-26 19:45:16 +00:00
|
|
|
hotReloader,
|
2017-04-18 04:18:43 +00:00
|
|
|
assetPrefix,
|
2016-10-10 04:24:30 +00:00
|
|
|
dir = process.cwd(),
|
|
|
|
dev = false,
|
2016-10-15 19:49:42 +00:00
|
|
|
staticMarkup = false
|
2016-10-10 04:24:30 +00:00
|
|
|
} = {}) {
|
2016-12-16 20:33:08 +00:00
|
|
|
page = page || pathname
|
2017-02-26 19:45:16 +00:00
|
|
|
|
|
|
|
await ensurePage(page, { dir, hotReloader })
|
|
|
|
|
2017-04-06 10:09:26 +00:00
|
|
|
const dist = getConfig(dir).distDir
|
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
let [Component, Document] = await Promise.all([
|
2017-04-06 10:09:26 +00:00
|
|
|
requireModule(join(dir, dist, 'dist', 'pages', page)),
|
|
|
|
requireModule(join(dir, dist, 'dist', 'pages', '_document'))
|
2016-12-16 18:42:40 +00:00
|
|
|
])
|
|
|
|
Component = Component.default || Component
|
|
|
|
Document = Document.default || Document
|
2017-05-03 16:40:09 +00:00
|
|
|
const asPath = req.url
|
|
|
|
const ctx = { err, req, res, pathname, query, asPath }
|
2017-04-18 04:18:43 +00:00
|
|
|
const props = await loadGetInitialProps(Component, ctx)
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
// the response might be finshed on the getinitialprops call
|
|
|
|
if (res.finished) return
|
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
const renderPage = () => {
|
2016-10-06 02:42:55 +00:00
|
|
|
const app = createElement(App, {
|
|
|
|
Component,
|
|
|
|
props,
|
2016-12-20 17:27:43 +00:00
|
|
|
router: new Router(pathname, query)
|
2016-10-06 02:42:55 +00:00
|
|
|
})
|
2016-12-20 17:27:43 +00:00
|
|
|
|
2016-12-21 15:57:25 +00:00
|
|
|
const render = staticMarkup ? renderToStaticMarkup : renderToString
|
|
|
|
|
|
|
|
let html
|
|
|
|
let head
|
2017-04-01 21:03:40 +00:00
|
|
|
let errorHtml = ''
|
2016-12-21 15:57:25 +00:00
|
|
|
try {
|
|
|
|
html = render(app)
|
|
|
|
} finally {
|
|
|
|
head = Head.rewind() || defaultHead()
|
|
|
|
}
|
2017-04-01 21:03:40 +00:00
|
|
|
|
|
|
|
if (err && dev) {
|
|
|
|
errorHtml = render(createElement(ErrorDebug, { error: err }))
|
|
|
|
}
|
|
|
|
|
|
|
|
return { html, head, errorHtml }
|
2016-12-16 18:42:40 +00:00
|
|
|
}
|
2016-10-06 02:42:55 +00:00
|
|
|
|
2017-01-20 19:33:46 +00:00
|
|
|
const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage })
|
2017-05-25 16:28:08 +00:00
|
|
|
// While developing, we should not cache any assets.
|
|
|
|
// So, we use a different buildId for each page load.
|
|
|
|
// With that we can ensure, we have unique URL for assets per every page load.
|
|
|
|
// So, it'll prevent issues like this: https://git.io/vHLtb
|
|
|
|
const devBuildId = Date.now()
|
2016-12-16 18:42:40 +00:00
|
|
|
|
2017-01-26 11:06:50 +00:00
|
|
|
if (res.finished) return
|
|
|
|
|
2017-05-19 22:02:01 +00:00
|
|
|
if (!Document.prototype || !Document.prototype.isReactComponent) throw new Error('_document.js is not exporting a React element')
|
2016-10-05 23:52:50 +00:00
|
|
|
const doc = createElement(Document, {
|
2016-12-16 18:42:40 +00:00
|
|
|
__NEXT_DATA__: {
|
2016-10-09 09:50:41 +00:00
|
|
|
props,
|
2016-12-16 20:33:08 +00:00
|
|
|
pathname,
|
|
|
|
query,
|
2017-05-25 16:28:08 +00:00
|
|
|
buildId: dev ? devBuildId : buildId,
|
2017-03-07 18:43:56 +00:00
|
|
|
buildStats,
|
2017-04-18 04:18:43 +00:00
|
|
|
assetPrefix,
|
2017-04-25 04:15:58 +00:00
|
|
|
err: (err) ? serializeError(dev, err) : null
|
2016-10-09 09:50:41 +00:00
|
|
|
},
|
2016-10-10 04:24:30 +00:00
|
|
|
dev,
|
2016-10-28 14:38:24 +00:00
|
|
|
staticMarkup,
|
2016-12-16 18:42:40 +00:00
|
|
|
...docProps
|
2016-10-05 23:52:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
|
|
|
|
}
|
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
export async function renderScript (req, res, page, opts) {
|
|
|
|
try {
|
2017-05-02 00:42:01 +00:00
|
|
|
const dist = getConfig(opts.dir).distDir
|
|
|
|
const path = join(opts.dir, dist, 'bundles', 'pages', page)
|
2017-04-18 04:18:43 +00:00
|
|
|
const realPath = await resolvePath(path)
|
|
|
|
await serveStatic(req, res, realPath)
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
renderScriptError(req, res, page, err, {}, opts)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err
|
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
2016-10-10 04:24:30 +00:00
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
export async function renderScriptError (req, res, page, error, customFields, opts) {
|
2017-04-24 20:48:34 +00:00
|
|
|
// Asks CDNs and others to not to cache the errored page
|
|
|
|
res.setHeader('Cache-Control', 'no-store, must-revalidate')
|
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
if (error.code === 'ENOENT') {
|
|
|
|
res.setHeader('Content-Type', 'text/javascript')
|
|
|
|
res.end(`
|
|
|
|
window.__NEXT_REGISTER_PAGE('${page}', function() {
|
2017-04-18 13:44:26 +00:00
|
|
|
var error = new Error('Page does not exist: ${page}')
|
2017-04-18 04:18:43 +00:00
|
|
|
error.statusCode = 404
|
|
|
|
|
|
|
|
return { error: error }
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
return
|
|
|
|
}
|
2016-12-31 12:46:23 +00:00
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
res.setHeader('Content-Type', 'text/javascript')
|
|
|
|
const errorJson = {
|
|
|
|
...errorToJSON(error),
|
|
|
|
...customFields
|
|
|
|
}
|
|
|
|
|
|
|
|
res.end(`
|
|
|
|
window.__NEXT_REGISTER_PAGE('${page}', function() {
|
|
|
|
var error = ${JSON.stringify(errorJson)}
|
|
|
|
return { error: error }
|
|
|
|
})
|
|
|
|
`)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 16:28:08 +00:00
|
|
|
export function sendHTML (req, res, html, method, { dev }) {
|
2016-12-30 19:55:20 +00:00
|
|
|
if (res.finished) return
|
2017-04-18 15:45:42 +00:00
|
|
|
const etag = generateETag(html)
|
2016-12-30 19:55:20 +00:00
|
|
|
|
2017-04-18 15:45:42 +00:00
|
|
|
if (fresh(req.headers, { etag })) {
|
|
|
|
res.statusCode = 304
|
|
|
|
res.end()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-25 16:28:08 +00:00
|
|
|
if (dev) {
|
|
|
|
// In dev, we should not cache pages for any reason.
|
|
|
|
// That's why we do this.
|
|
|
|
res.setHeader('Cache-Control', 'no-store, must-revalidate')
|
|
|
|
}
|
|
|
|
|
2017-04-18 15:45:42 +00:00
|
|
|
res.setHeader('ETag', etag)
|
2016-12-16 20:33:08 +00:00
|
|
|
res.setHeader('Content-Type', 'text/html')
|
|
|
|
res.setHeader('Content-Length', Buffer.byteLength(html))
|
2017-01-12 15:38:43 +00:00
|
|
|
res.end(method === 'HEAD' ? null : html)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 15:38:43 +00:00
|
|
|
export function sendJSON (res, obj, method) {
|
2016-12-30 19:55:20 +00:00
|
|
|
if (res.finished) return
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
const json = JSON.stringify(obj)
|
|
|
|
res.setHeader('Content-Type', 'application/json')
|
|
|
|
res.setHeader('Content-Length', Buffer.byteLength(json))
|
2017-01-12 15:38:43 +00:00
|
|
|
res.end(method === 'HEAD' ? null : json)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function errorToJSON (err) {
|
2016-10-19 12:41:45 +00:00
|
|
|
const { name, message, stack } = err
|
|
|
|
const json = { name, message, stack }
|
|
|
|
|
2017-03-22 23:13:23 +00:00
|
|
|
if (err.module) {
|
|
|
|
// rawRequest contains the filename of the module which has the error.
|
|
|
|
const { rawRequest } = err.module
|
2016-10-19 12:41:45 +00:00
|
|
|
json.module = { rawRequest }
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
2016-12-31 12:46:23 +00:00
|
|
|
|
2017-04-25 04:15:58 +00:00
|
|
|
function serializeError (dev, err) {
|
|
|
|
if (dev) {
|
|
|
|
return errorToJSON(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return { message: '500 - Internal Server Error.' }
|
|
|
|
}
|
|
|
|
|
2016-12-31 12:46:23 +00:00
|
|
|
export function serveStatic (req, res, path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
send(req, path)
|
2017-02-08 00:13:25 +00:00
|
|
|
.on('directory', () => {
|
|
|
|
// We don't allow directories to be read.
|
|
|
|
const err = new Error('No directory access')
|
|
|
|
err.code = 'ENOENT'
|
|
|
|
reject(err)
|
|
|
|
})
|
2017-01-01 19:36:37 +00:00
|
|
|
.on('error', reject)
|
2016-12-31 12:46:23 +00:00
|
|
|
.pipe(res)
|
|
|
|
.on('finish', resolve)
|
|
|
|
})
|
|
|
|
}
|
2017-02-26 19:45:16 +00:00
|
|
|
|
|
|
|
async function ensurePage (page, { dir, hotReloader }) {
|
|
|
|
if (!hotReloader) return
|
|
|
|
if (page === '_error' || page === '_document') return
|
|
|
|
|
|
|
|
await hotReloader.ensurePage(page)
|
|
|
|
}
|