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
2016-10-08 14:12:51 +09:00

51 lines
1.4 KiB
JavaScript

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 Head from '../lib/head'
import App from '../lib/app'
import { StyleSheetServer } from '../lib/css'
export async function render (path, req, res, { dir = process.cwd(), dev = false } = {}) {
const mod = require(resolve(dir, '.next', 'pages', path)) || {}
const Component = mod.default
let props = {}
if (Component.getInitialProps) {
props = await Component.getInitialProps({ req, res })
}
const bundlePath = resolve(dir, '.next', '_bundles', '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 head = Head.rewind() || []
const doc = createElement(Document, {
html,
head,
css,
data: { component, props },
hotReload: false,
dev
})
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
}
export async function renderJSON (path, { dir = process.cwd() }) {
const bundlePath = resolve(dir, '.next', '_bundles', 'pages', (path || 'index') + '.js')
const component = await fs.readFile(bundlePath, 'utf8')
return { component }
}