2017-01-12 01:58:20 +00:00
|
|
|
import { createElement } from 'react'
|
|
|
|
import ReactDOM from 'react-dom'
|
2017-03-13 04:46:32 +00:00
|
|
|
import mitt from 'mitt'
|
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-04-01 21:03:40 +00:00
|
|
|
import ErrorDebugComponent from '../lib/error-debug'
|
2017-04-03 18:10:24 +00:00
|
|
|
import PageLoader from '../lib/page-loader'
|
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,
|
2017-04-03 18:10:24 +00:00
|
|
|
query,
|
|
|
|
buildId
|
2017-02-28 17:31:17 +00:00
|
|
|
},
|
|
|
|
location
|
2017-01-12 01:58:20 +00:00
|
|
|
} = window
|
|
|
|
|
2017-04-04 21:08:38 +00:00
|
|
|
const pageLoader = window.NEXT_PAGE_LOADER = new PageLoader(buildId)
|
2017-01-12 01:58:20 +00:00
|
|
|
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-04-04 21:08:38 +00:00
|
|
|
pageLoader,
|
2017-01-12 01:58:20 +00:00
|
|
|
Component,
|
|
|
|
ErrorComponent,
|
|
|
|
err
|
|
|
|
})
|
|
|
|
|
|
|
|
const headManager = new HeadManager()
|
2017-04-01 21:03:40 +00:00
|
|
|
const appContainer = document.getElementById('__next')
|
|
|
|
const errorContainer = document.getElementById('__next-error')
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2017-04-01 21:03:40 +00:00
|
|
|
export default () => {
|
2017-03-13 04:46:32 +00:00
|
|
|
const emitter = mitt()
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
router.subscribe(({ Component, props, hash, err }) => {
|
2017-04-01 21:03:40 +00:00
|
|
|
render({ Component, props, err, hash, emitter })
|
2017-01-12 01:58:20 +00:00
|
|
|
})
|
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
const hash = location.hash.substring(1)
|
2017-04-01 21:03:40 +00:00
|
|
|
render({ Component, props, hash, err, emitter })
|
2017-01-31 03:26:17 +00:00
|
|
|
|
|
|
|
return emitter
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 21:03:40 +00:00
|
|
|
export async function render (props) {
|
2017-04-04 19:55:56 +00:00
|
|
|
// There are some errors we should ignore.
|
|
|
|
// Next.js rendering logic knows how to handle them.
|
|
|
|
// These are specially 404 errors
|
|
|
|
if (props.err && !props.err.ignore) {
|
2017-04-01 21:03:40 +00:00
|
|
|
await renderError(props.err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
try {
|
|
|
|
await doRender(props)
|
|
|
|
} catch (err) {
|
2017-04-01 21:03:40 +00:00
|
|
|
if (err.abort) return
|
|
|
|
await renderError(err)
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 21:03:40 +00:00
|
|
|
// This method handles all runtime and debug errors.
|
|
|
|
// 404 and 500 errors are special kind of errors
|
|
|
|
// and they are still handle via the main render method.
|
|
|
|
export async function renderError (error) {
|
|
|
|
const prod = process.env.NODE_ENV === 'production'
|
|
|
|
// We need to unmount the current app component because it's
|
|
|
|
// in the inconsistant state.
|
|
|
|
// Otherwise, we need to face issues when the issue is fixed and
|
|
|
|
// it's get notified via HMR
|
|
|
|
ReactDOM.unmountComponentAtNode(appContainer)
|
|
|
|
|
|
|
|
const errorMessage = `${error.message}\n${error.stack}`
|
|
|
|
console.error(errorMessage)
|
|
|
|
|
|
|
|
if (prod) {
|
|
|
|
const initProps = { err: error, pathname, query }
|
|
|
|
const props = await loadGetInitialProps(ErrorComponent, initProps)
|
|
|
|
ReactDOM.render(createElement(ErrorComponent, props), errorContainer)
|
|
|
|
} else {
|
|
|
|
ReactDOM.render(createElement(ErrorDebugComponent, { error }), errorContainer)
|
|
|
|
}
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
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-04-01 21:03:40 +00:00
|
|
|
// We need to clear any existing runtime error messages
|
|
|
|
ReactDOM.unmountComponentAtNode(errorContainer)
|
|
|
|
ReactDOM.render(createElement(App, appProps), appContainer)
|
2017-01-31 03:26:17 +00:00
|
|
|
|
|
|
|
if (emitter) {
|
|
|
|
emitter.emit('after-reactdom-render', { Component })
|
|
|
|
}
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|