rsnext/server/render.js

77 lines
2 KiB
JavaScript
Raw Normal View History

2016-10-19 14:58:08 +02:00
import { join } from 'path'
2016-10-10 06:24:30 +02:00
import { parse } from 'url'
2016-10-06 01:52:50 +02:00
import { createElement } from 'react'
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
2016-10-15 21:49:42 +02:00
import requireModule from './require'
2016-10-08 14:01:58 +02:00
import read from './read'
2016-10-08 12:16:22 +02:00
import Router from '../lib/router'
2016-10-06 01:52:50 +02:00
import Document from '../lib/document'
2016-10-07 03:57:31 +02:00
import Head from '../lib/head'
2016-10-06 01:52:50 +02:00
import App from '../lib/app'
2016-10-06 04:42:55 +02:00
import { StyleSheetServer } from '../lib/css'
2016-10-06 01:52:50 +02:00
2016-10-10 06:24:30 +02:00
export async function render (url, ctx = {}, {
dir = process.cwd(),
dev = false,
2016-10-15 21:49:42 +02:00
staticMarkup = false
2016-10-10 06:24:30 +02:00
} = {}) {
const path = getPath(url)
2016-10-19 14:58:08 +02:00
const mod = await requireModule(join(dir, '.next', 'dist', 'pages', path))
2016-10-08 12:16:22 +02:00
const Component = mod.default || mod
2016-10-06 01:52:50 +02:00
2016-10-09 11:25:38 +02:00
const props = await (Component.getInitialProps ? Component.getInitialProps(ctx) : {})
2016-10-19 14:58:08 +02:00
const component = await read(join(dir, '.next', 'bundles', 'pages', path))
2016-10-06 01:52:50 +02:00
2016-10-06 04:42:55 +02:00
const { html, css } = StyleSheetServer.renderStatic(() => {
const app = createElement(App, {
Component,
props,
2016-10-10 06:24:30 +02:00
router: new Router(ctx.req ? ctx.req.url : url)
2016-10-06 04:42:55 +02:00
})
2016-10-10 06:24:30 +02:00
return (staticMarkup ? renderToStaticMarkup : renderToString)(app)
2016-10-06 01:52:50 +02:00
})
2016-10-07 03:57:31 +02:00
const head = Head.rewind() || []
2016-10-06 01:52:50 +02:00
const doc = createElement(Document, {
2016-10-07 03:57:31 +02:00
html,
head,
css,
2016-10-09 11:50:41 +02:00
data: {
component,
props,
2016-10-19 14:41:45 +02:00
classNames: css.renderedClassNames,
err: ctx.err ? errorToJSON(ctx.err) : null
2016-10-09 11:50:41 +02:00
},
2016-10-06 13:05:52 +02:00
hotReload: false,
2016-10-10 06:24:30 +02:00
dev,
staticMarkup
2016-10-06 01:52:50 +02:00
})
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
}
2016-10-15 21:49:42 +02:00
export async function renderJSON (url, { dir = process.cwd() } = {}) {
2016-10-10 06:24:30 +02:00
const path = getPath(url)
2016-10-19 14:58:08 +02:00
const component = await read(join(dir, '.next', 'bundles', 'pages', path))
2016-10-06 09:06:55 +02:00
return { component }
2016-10-06 01:52:50 +02:00
}
2016-10-10 06:24:30 +02:00
2016-10-19 14:41:45 +02: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 06:24:30 +02:00
function getPath (url) {
2016-10-19 14:58:08 +02:00
return parse(url || '/').pathname.replace(/\.json$/, '')
2016-10-10 06:24:30 +02:00
}