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-10-15 19:49:42 +00:00
|
|
|
import requireModule from './require'
|
2016-10-08 12:01:58 +00:00
|
|
|
import read from './read'
|
2016-12-19 14:40:26 +00:00
|
|
|
import { createRouter } from '../lib/router'
|
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'
|
|
|
|
|
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)
|
|
|
|
sendHTML(res, html)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
sendHTML(res, html)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderErrorToHTML (err, req, res, pathname, query, opts = {}) {
|
|
|
|
const page = err && opts.dev ? '/_error-debug' : '/_error'
|
|
|
|
return doRender(req, res, pathname, query, { ...opts, err, page })
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doRender (req, res, pathname, query, {
|
|
|
|
err,
|
|
|
|
page,
|
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
|
2016-12-16 18:42:40 +00:00
|
|
|
let [Component, Document] = await Promise.all([
|
2016-12-16 20:33:08 +00:00
|
|
|
requireModule(join(dir, '.next', 'dist', 'pages', page)),
|
2016-12-16 18:42:40 +00:00
|
|
|
requireModule(join(dir, '.next', 'dist', 'pages', '_document'))
|
|
|
|
])
|
|
|
|
Component = Component.default || Component
|
|
|
|
Document = Document.default || Document
|
2016-12-16 20:33:08 +00:00
|
|
|
const ctx = { err, req, res, pathname, query }
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-11-24 14:03:16 +00:00
|
|
|
const [
|
|
|
|
props,
|
|
|
|
component,
|
|
|
|
errorComponent
|
|
|
|
] = await Promise.all([
|
|
|
|
Component.getInitialProps ? Component.getInitialProps(ctx) : {},
|
2016-12-16 20:33:08 +00:00
|
|
|
read(join(dir, '.next', 'bundles', 'pages', page)),
|
2016-11-24 14:03:16 +00:00
|
|
|
read(join(dir, '.next', 'bundles', 'pages', dev ? '_error-debug' : '_error'))
|
|
|
|
])
|
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-12-19 14:40:26 +00:00
|
|
|
const router = createRouter(pathname, query)
|
2016-10-06 02:42:55 +00:00
|
|
|
const app = createElement(App, {
|
|
|
|
Component,
|
|
|
|
props,
|
2016-12-19 14:40:26 +00:00
|
|
|
router
|
2016-10-06 02:42:55 +00:00
|
|
|
})
|
2016-12-16 18:42:40 +00:00
|
|
|
const html = (staticMarkup ? renderToStaticMarkup : renderToString)(app)
|
|
|
|
const head = Head.rewind() || defaultHead()
|
|
|
|
return { html, head }
|
|
|
|
}
|
2016-10-06 02:42:55 +00:00
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
const docProps = await Document.getInitialProps({ ...ctx, renderPage })
|
|
|
|
|
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
|
|
|
component,
|
2016-11-24 14:03:16 +00:00
|
|
|
errorComponent,
|
2016-10-09 09:50:41 +00:00
|
|
|
props,
|
2016-12-16 20:33:08 +00:00
|
|
|
pathname,
|
|
|
|
query,
|
|
|
|
err: (err && dev) ? errorToJSON(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)
|
|
|
|
}
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
export async function renderJSON (res, page, { dir = process.cwd() } = {}) {
|
|
|
|
const component = await read(join(dir, '.next', 'bundles', 'pages', page))
|
|
|
|
sendJSON(res, { component })
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
2016-10-10 04:24:30 +00:00
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
export async function renderErrorJSON (err, res, { dir = process.cwd(), dev = false } = {}) {
|
|
|
|
const page = err && dev ? '/_error-debug' : '/_error'
|
|
|
|
const component = await read(join(dir, '.next', 'bundles', 'pages', page))
|
|
|
|
sendJSON(res, {
|
|
|
|
component,
|
|
|
|
err: err && dev ? errorToJSON(err) : null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function sendHTML (res, html) {
|
|
|
|
res.setHeader('Content-Type', 'text/html')
|
|
|
|
res.setHeader('Content-Length', Buffer.byteLength(html))
|
|
|
|
res.end(html)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function sendJSON (res, obj) {
|
|
|
|
const json = JSON.stringify(obj)
|
|
|
|
res.setHeader('Content-Type', 'application/json')
|
|
|
|
res.setHeader('Content-Length', Buffer.byteLength(json))
|
|
|
|
res.end(json)
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorToJSON (err) {
|
2016-10-19 12:41:45 +00:00
|
|
|
const { name, message, stack } = err
|
|
|
|
const json = { name, message, stack }
|
|
|
|
|
|
|
|
if (name === 'ModuleBuildError') {
|
|
|
|
// webpack compilation error
|
|
|
|
const { module: { rawRequest } } = err
|
|
|
|
json.module = { rawRequest }
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|