1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/render.js

88 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-10-19 12:58:08 +00:00
import { join } from 'path'
2016-10-10 04:24:30 +00:00
import { parse } from 'url'
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-10-28 14:38:24 +00:00
import getConfig from './config'
2016-10-08 10:16:22 +00:00
import Router 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-10-10 04:24:30 +00:00
export async function render (url, ctx = {}, {
dir = process.cwd(),
dev = false,
2016-10-15 19:49:42 +00:00
staticMarkup = false
2016-10-10 04:24:30 +00:00
} = {}) {
const path = getPath(url)
2016-12-16 18:42:40 +00:00
let [Component, Document] = await Promise.all([
requireModule(join(dir, '.next', 'dist', 'pages', path)),
requireModule(join(dir, '.next', 'dist', 'pages', '_document'))
])
Component = Component.default || Component
Document = Document.default || Document
2016-10-05 23:52:50 +00:00
const [
props,
component,
errorComponent
] = await Promise.all([
Component.getInitialProps ? Component.getInitialProps(ctx) : {},
read(join(dir, '.next', 'bundles', 'pages', path)),
read(join(dir, '.next', 'bundles', 'pages', dev ? '_error-debug' : '_error'))
])
2016-10-05 23:52:50 +00:00
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-10-10 04:24:30 +00:00
router: new Router(ctx.req ? ctx.req.url : url)
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-10-28 14:38:24 +00:00
const config = await getConfig(dir)
2016-10-07 01:57:31 +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,
errorComponent,
2016-10-09 09:50:41 +00:00
props,
err: (ctx.err && dev) ? errorToJSON(ctx.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
cdn: config.cdn,
...docProps
2016-10-05 23:52:50 +00:00
})
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
}
2016-10-15 19:49:42 +00:00
export async function renderJSON (url, { dir = process.cwd() } = {}) {
2016-10-10 04:24:30 +00:00
const path = getPath(url)
2016-10-19 12:58:08 +00:00
const component = await read(join(dir, '.next', 'bundles', 'pages', path))
2016-10-06 07:06:55 +00:00
return { component }
2016-10-05 23:52:50 +00:00
}
2016-10-10 04:24:30 +00:00
2016-10-19 12:41:45 +00:00
export function errorToJSON (err) {
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-10-10 04:24:30 +00:00
function getPath (url) {
2016-10-19 12:58:08 +00:00
return parse(url || '/').pathname.replace(/\.json$/, '')
2016-10-10 04:24:30 +00:00
}