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 { createRouter } from '../lib/router'
|
|
|
|
import App from '../lib/app'
|
|
|
|
import evalScript from '../lib/eval-script'
|
2017-02-28 17:31:17 +00:00
|
|
|
import { loadGetInitialProps, getURL } from '../lib/utils'
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2017-03-04 16:48:33 +00:00
|
|
|
// Polyfill Promise globally
|
|
|
|
// This is needed because Webpack2's dynamic loading(common chunks) code
|
|
|
|
// depends on Promise.
|
|
|
|
// So, we need to polyfill it.
|
|
|
|
// See: https://github.com/webpack/webpack/issues/4254
|
|
|
|
if (!window.Promise) {
|
|
|
|
window.Promise = Promise
|
|
|
|
}
|
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
const {
|
|
|
|
__NEXT_DATA__: {
|
|
|
|
component,
|
|
|
|
errorComponent,
|
|
|
|
props,
|
|
|
|
err,
|
|
|
|
pathname,
|
|
|
|
query
|
2017-02-28 17:31:17 +00:00
|
|
|
},
|
|
|
|
location
|
2017-01-12 01:58:20 +00:00
|
|
|
} = window
|
|
|
|
|
|
|
|
const Component = evalScript(component).default
|
|
|
|
const ErrorComponent = evalScript(errorComponent).default
|
|
|
|
let lastAppProps
|
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
export const router = createRouter(pathname, query, getURL(), {
|
2017-01-12 01:58:20 +00:00
|
|
|
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
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
router.subscribe(({ Component, props, hash, err }) => {
|
|
|
|
render({ Component, props, err, hash, emitter }, onError)
|
2017-01-12 01:58:20 +00:00
|
|
|
})
|
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
const hash = location.hash.substring(1)
|
|
|
|
render({ Component, props, hash, err, emitter }, onError)
|
2017-01-31 03:26:17 +00:00
|
|
|
|
|
|
|
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-02-28 17:31:17 +00:00
|
|
|
async function doRender ({ Component, props, hash, 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
|
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
const appProps = { Component, props, hash, err, router, headManager }
|
2017-02-19 20:35:48 +00:00
|
|
|
// lastAppProps has to be set before ReactDom.render to account for ReactDom throwing an error.
|
|
|
|
lastAppProps = appProps
|
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
ReactDOM.render(createElement(App, appProps), container)
|
2017-01-31 03:26:17 +00:00
|
|
|
|
|
|
|
if (emitter) {
|
|
|
|
emitter.emit('after-reactdom-render', { Component })
|
|
|
|
}
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|