2017-01-12 01:58:20 +00:00
|
|
|
import { createElement } from 'react'
|
|
|
|
import ReactDOM from 'react-dom'
|
2017-01-31 03:26:17 +00:00
|
|
|
import { EventEmitter } from 'events'
|
2017-01-12 01:58:20 +00:00
|
|
|
import HeadManager from './head-manager'
|
|
|
|
import { rehydrate } from '../lib/css'
|
|
|
|
import { createRouter } from '../lib/router'
|
|
|
|
import App from '../lib/app'
|
|
|
|
import evalScript from '../lib/eval-script'
|
2017-01-20 19:33:46 +00:00
|
|
|
import { loadGetInitialProps } from '../lib/utils'
|
2017-01-12 01:58:20 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
__NEXT_DATA__: {
|
|
|
|
component,
|
|
|
|
errorComponent,
|
|
|
|
props,
|
|
|
|
ids,
|
|
|
|
err,
|
|
|
|
pathname,
|
|
|
|
query
|
|
|
|
}
|
|
|
|
} = window
|
|
|
|
|
|
|
|
const Component = evalScript(component).default
|
|
|
|
const ErrorComponent = evalScript(errorComponent).default
|
|
|
|
let lastAppProps
|
|
|
|
|
|
|
|
export const router = createRouter(pathname, query, {
|
|
|
|
Component,
|
|
|
|
ErrorComponent,
|
|
|
|
err
|
|
|
|
})
|
|
|
|
|
|
|
|
const headManager = new HeadManager()
|
|
|
|
const container = document.getElementById('__next')
|
|
|
|
|
|
|
|
export default (onError) => {
|
2017-01-31 03:26:17 +00:00
|
|
|
const emitter = new EventEmitter()
|
2017-01-12 01:58:20 +00:00
|
|
|
if (ids && ids.length) rehydrate(ids)
|
|
|
|
|
|
|
|
router.subscribe(({ Component, props, err }) => {
|
2017-01-31 03:26:17 +00:00
|
|
|
render({ Component, props, err, emitter }, onError)
|
2017-01-12 01:58:20 +00:00
|
|
|
})
|
|
|
|
|
2017-01-31 03:26:17 +00:00
|
|
|
render({ Component, props, err, emitter }, onError)
|
|
|
|
|
|
|
|
return emitter
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function render (props, onError = renderErrorComponent) {
|
|
|
|
try {
|
|
|
|
await doRender(props)
|
|
|
|
} catch (err) {
|
|
|
|
await onError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function renderErrorComponent (err) {
|
|
|
|
const { pathname, query } = router
|
2017-01-20 19:33:46 +00:00
|
|
|
const props = await loadGetInitialProps(ErrorComponent, { err, pathname, query })
|
2017-01-12 01:58:20 +00:00
|
|
|
await doRender({ Component: ErrorComponent, props, err })
|
|
|
|
}
|
|
|
|
|
2017-01-31 03:26:17 +00:00
|
|
|
async function doRender ({ Component, props, err, emitter }) {
|
2017-01-12 01:58:20 +00:00
|
|
|
if (!props && Component &&
|
|
|
|
Component !== ErrorComponent &&
|
|
|
|
lastAppProps.Component === ErrorComponent) {
|
|
|
|
// fetch props if ErrorComponent was replaced with a page component by HMR
|
|
|
|
const { pathname, query } = router
|
2017-01-20 19:33:46 +00:00
|
|
|
props = await loadGetInitialProps(Component, { err, pathname, query })
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 03:26:17 +00:00
|
|
|
if (emitter) {
|
|
|
|
emitter.emit('before-reactdom-render', { Component })
|
|
|
|
}
|
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
Component = Component || lastAppProps.Component
|
|
|
|
props = props || lastAppProps.props
|
|
|
|
|
|
|
|
const appProps = { Component, props, err, router, headManager }
|
|
|
|
ReactDOM.render(createElement(App, appProps), container)
|
2017-01-31 03:26:17 +00:00
|
|
|
|
|
|
|
if (emitter) {
|
|
|
|
emitter.emit('after-reactdom-render', { Component })
|
|
|
|
}
|
|
|
|
|
|
|
|
lastAppProps = appProps
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|