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-01-09 03:01:25 +00:00
|
|
|
import fs from 'mz/fs'
|
2016-12-31 12:46:23 +00:00
|
|
|
import accepts from 'accepts'
|
2017-01-09 02:12:10 +00:00
|
|
|
import mime from 'mime-types'
|
2016-10-15 19:49:42 +00:00
|
|
|
import requireModule from './require'
|
2017-01-01 10:07:56 +00:00
|
|
|
import resolvePath from './resolve'
|
2016-12-31 12:46:23 +00:00
|
|
|
import readPage from './read-page'
|
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'
|
|
|
|
|
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-01-12 15:38:43 +00:00
|
|
|
sendHTML(res, html, req.method)
|
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-01-12 15:38:43 +00:00
|
|
|
sendHTML(res, html, req.method)
|
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,
|
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([
|
2017-01-20 19:33:46 +00:00
|
|
|
loadGetInitialProps(Component, ctx),
|
2016-12-31 12:46:23 +00:00
|
|
|
readPage(join(dir, '.next', 'bundles', 'pages', page)),
|
2017-01-12 01:58:20 +00:00
|
|
|
readPage(join(dir, '.next', 'bundles', 'pages', '_error'))
|
2016-11-24 14:03:16 +00:00
|
|
|
])
|
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,
|
2017-01-12 01:58:20 +00:00
|
|
|
err,
|
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
|
|
|
|
try {
|
|
|
|
html = render(app)
|
|
|
|
} finally {
|
|
|
|
head = Head.rewind() || defaultHead()
|
|
|
|
}
|
2016-12-16 18:42:40 +00:00
|
|
|
return { html, head }
|
|
|
|
}
|
2016-10-06 02:42:55 +00:00
|
|
|
|
2017-01-20 19:33:46 +00:00
|
|
|
const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage })
|
2016-12-16 18:42:40 +00:00
|
|
|
|
2017-01-26 11:06:50 +00:00
|
|
|
if (res.finished) return
|
|
|
|
|
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,
|
2017-01-11 20:16:18 +00:00
|
|
|
buildId,
|
2016-12-16 20:33:08 +00:00
|
|
|
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-31 12:46:23 +00:00
|
|
|
export async function renderJSON (req, res, page, { dir = process.cwd() } = {}) {
|
2017-01-01 10:07:56 +00:00
|
|
|
const pagePath = await resolvePath(join(dir, '.next', 'bundles', 'pages', page))
|
2016-12-31 12:46:23 +00:00
|
|
|
return serveStaticWithGzip(req, res, pagePath)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
2016-10-10 04:24:30 +00:00
|
|
|
|
2016-12-31 12:46:23 +00:00
|
|
|
export async function renderErrorJSON (err, req, res, { dir = process.cwd(), dev = false } = {}) {
|
2017-01-12 01:58:20 +00:00
|
|
|
const component = await readPage(join(dir, '.next', 'bundles', 'pages', '_error'))
|
2016-12-31 12:46:23 +00:00
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
sendJSON(res, {
|
|
|
|
component,
|
|
|
|
err: err && dev ? errorToJSON(err) : null
|
2017-01-12 15:38:43 +00:00
|
|
|
}, req.method)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 15:38:43 +00:00
|
|
|
export function sendHTML (res, html, method) {
|
2016-12-30 19:55:20 +00:00
|
|
|
if (res.finished) return
|
|
|
|
|
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 }
|
|
|
|
|
|
|
|
if (name === 'ModuleBuildError') {
|
|
|
|
// webpack compilation error
|
|
|
|
const { module: { rawRequest } } = err
|
|
|
|
json.module = { rawRequest }
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
2016-12-31 12:46:23 +00:00
|
|
|
|
|
|
|
export async function serveStaticWithGzip (req, res, path) {
|
|
|
|
const encoding = accepts(req).encodings(['gzip'])
|
|
|
|
if (encoding !== 'gzip') {
|
|
|
|
return serveStatic(req, res, path)
|
|
|
|
}
|
|
|
|
|
2017-01-09 03:01:25 +00:00
|
|
|
const gzipPath = `${path}.gz`
|
|
|
|
|
2016-12-31 12:46:23 +00:00
|
|
|
try {
|
2017-01-09 03:01:25 +00:00
|
|
|
// We need to check the existance of the gzipPath.
|
|
|
|
// Getting `ENOENT` error from the `serveStatic` is inconsistent and
|
|
|
|
// didn't work on all the cases.
|
|
|
|
//
|
|
|
|
// And this won't give us a race condition because we know that
|
|
|
|
// we don't add gzipped files at runtime.
|
|
|
|
await fs.stat(gzipPath)
|
2016-12-31 12:46:23 +00:00
|
|
|
} catch (ex) {
|
2017-01-12 04:14:49 +00:00
|
|
|
// Handles the error thrown by fs.stat
|
2016-12-31 12:46:23 +00:00
|
|
|
if (ex.code === 'ENOENT') {
|
2017-01-09 03:01:25 +00:00
|
|
|
// Seems like there's no gzipped file. Let's serve the uncompressed file.
|
2016-12-31 12:46:23 +00:00
|
|
|
return serveStatic(req, res, path)
|
|
|
|
}
|
2017-01-09 03:01:25 +00:00
|
|
|
|
2016-12-31 12:46:23 +00:00
|
|
|
throw ex
|
|
|
|
}
|
2017-01-09 03:01:25 +00:00
|
|
|
|
|
|
|
const contentType = mime.lookup(path) || 'application/octet-stream'
|
|
|
|
res.setHeader('Content-Type', contentType)
|
|
|
|
res.setHeader('Content-Encoding', 'gzip')
|
|
|
|
return serveStatic(req, res, gzipPath)
|
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)
|
|
|
|
})
|
|
|
|
}
|