forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender.js
43 lines (35 loc) · 1.13 KB
/
render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { relative, resolve } from 'path'
import { createElement } from 'react'
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
import fs from 'mz/fs'
import Document from '../lib/document'
import App from '../lib/app'
import { StyleSheetServer } from '../lib/css'
export async function render (path, req, res, { root = process.cwd() } = {}) {
const mod = require(resolve(root, '.next', 'pages', path)) || {}
const Component = mod.default
let props = {}
if (Component.getInitialProps) {
props = await Component.getInitialProps({ req, res })
}
const bundlePath = resolve(root, '.next', '.next', 'pages', (path || 'index') + '.js')
const component = await fs.readFile(bundlePath, 'utf8')
const { html, css } = StyleSheetServer.renderStatic(() => {
const app = createElement(App, {
Component,
props,
router: {}
})
return renderToString(app)
})
const doc = createElement(Document, {
head: [],
html: html,
css: css,
data: { component },
hotReload: false
})
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
}
export async function renderJSON (path) {
}