2017-01-12 01:58:20 +00:00
|
|
|
import { createElement } from 'react'
|
|
|
|
import ReactDOM from 'react-dom'
|
|
|
|
import HeadManager from './head-manager'
|
|
|
|
import { createRouter } from '../lib/router'
|
2017-07-06 06:49:33 +00:00
|
|
|
import EventEmitter from '../lib/EventEmitter'
|
2017-01-12 01:58:20 +00:00
|
|
|
import App from '../lib/app'
|
2017-02-28 17:31:17 +00:00
|
|
|
import { loadGetInitialProps, getURL } from '../lib/utils'
|
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__: {
|
|
|
|
props,
|
|
|
|
err,
|
|
|
|
pathname,
|
2017-04-03 18:10:24 +00:00
|
|
|
query,
|
2017-04-17 20:15:50 +00:00
|
|
|
buildId,
|
2017-04-18 16:12:21 +00:00
|
|
|
chunks,
|
2017-04-18 04:18:43 +00:00
|
|
|
assetPrefix
|
2017-02-28 17:31:17 +00:00
|
|
|
},
|
|
|
|
location
|
2017-01-12 01:58:20 +00:00
|
|
|
} = window
|
|
|
|
|
2017-05-03 16:40:09 +00:00
|
|
|
const asPath = getURL()
|
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
const pageLoader = new PageLoader(buildId, assetPrefix)
|
2017-04-11 14:33:18 +00:00
|
|
|
window.__NEXT_LOADED_PAGES__.forEach(({ route, fn }) => {
|
|
|
|
pageLoader.registerPage(route, fn)
|
|
|
|
})
|
|
|
|
delete window.__NEXT_LOADED_PAGES__
|
|
|
|
|
2017-04-17 20:15:50 +00:00
|
|
|
window.__NEXT_LOADED_CHUNKS__.forEach(({ chunkName, fn }) => {
|
|
|
|
pageLoader.registerChunk(chunkName, fn)
|
|
|
|
})
|
|
|
|
delete window.__NEXT_LOADED_CHUNKS__
|
|
|
|
|
2017-04-11 14:33:18 +00:00
|
|
|
window.__NEXT_REGISTER_PAGE = pageLoader.registerPage.bind(pageLoader)
|
2017-04-17 20:15:50 +00:00
|
|
|
window.__NEXT_REGISTER_CHUNK = pageLoader.registerChunk.bind(pageLoader)
|
2017-04-05 06:45:39 +00:00
|
|
|
|
2017-04-06 06:11:13 +00:00
|
|
|
const headManager = new HeadManager()
|
|
|
|
const appContainer = document.getElementById('__next')
|
|
|
|
const errorContainer = document.getElementById('__next-error')
|
2017-04-05 11:43:34 +00:00
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
let lastAppProps
|
2017-04-06 06:11:13 +00:00
|
|
|
export let router
|
|
|
|
export let ErrorComponent
|
2017-10-15 21:19:01 +00:00
|
|
|
let ErrorDebugComponent
|
2017-04-06 06:11:13 +00:00
|
|
|
let Component
|
2017-10-21 10:58:59 +00:00
|
|
|
let stripAnsi = (s) => s
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2017-10-21 10:58:59 +00:00
|
|
|
export default async ({ ErrorDebugComponent: passedDebugComponent, stripAnsi: passedStripAnsi } = {}) => {
|
2017-04-17 20:15:50 +00:00
|
|
|
// Wait for all the dynamic chunks to get loaded
|
|
|
|
for (const chunkName of chunks) {
|
|
|
|
await pageLoader.waitForChunk(chunkName)
|
|
|
|
}
|
|
|
|
|
2017-10-21 10:58:59 +00:00
|
|
|
stripAnsi = passedStripAnsi || stripAnsi
|
2017-10-15 21:19:01 +00:00
|
|
|
ErrorDebugComponent = passedDebugComponent
|
2017-04-06 06:11:13 +00:00
|
|
|
ErrorComponent = await pageLoader.loadPage('/_error')
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2017-04-06 06:11:13 +00:00
|
|
|
try {
|
|
|
|
Component = await pageLoader.loadPage(pathname)
|
|
|
|
} catch (err) {
|
2017-10-21 10:58:59 +00:00
|
|
|
console.error(stripAnsi(`${err.message}\n${err.stack}`))
|
2017-04-06 06:11:13 +00:00
|
|
|
Component = ErrorComponent
|
|
|
|
}
|
|
|
|
|
2017-05-03 16:40:09 +00:00
|
|
|
router = createRouter(pathname, query, asPath, {
|
2017-04-06 06:11:13 +00:00
|
|
|
pageLoader,
|
|
|
|
Component,
|
|
|
|
ErrorComponent,
|
|
|
|
err
|
|
|
|
})
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2017-07-06 06:49:33 +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 }) => {
|
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}`
|
2017-10-21 10:58:59 +00:00
|
|
|
console.error(stripAnsi(errorMessage))
|
2017-04-01 21:03:40 +00:00
|
|
|
|
|
|
|
if (prod) {
|
2017-05-03 16:40:09 +00:00
|
|
|
const initProps = { err: error, pathname, query, asPath }
|
2017-04-01 21:03:40 +00:00
|
|
|
const props = await loadGetInitialProps(ErrorComponent, initProps)
|
2017-09-28 20:36:48 +00:00
|
|
|
renderReactElement(createElement(ErrorComponent, props), errorContainer)
|
2017-04-01 21:03:40 +00:00
|
|
|
} else {
|
2017-09-28 20:36:48 +00:00
|
|
|
renderReactElement(createElement(ErrorDebugComponent, { error }), errorContainer)
|
2017-04-01 21:03:40 +00:00
|
|
|
}
|
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
|
2017-05-03 16:40:09 +00:00
|
|
|
const { pathname, query, asPath } = router
|
|
|
|
props = await loadGetInitialProps(Component, { err, pathname, query, asPath })
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 03:26:17 +00:00
|
|
|
if (emitter) {
|
2017-04-06 06:11:13 +00:00
|
|
|
emitter.emit('before-reactdom-render', { Component, ErrorComponent })
|
2017-01-31 03:26:17 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2017-09-28 20:36:48 +00:00
|
|
|
renderReactElement(createElement(App, appProps), appContainer)
|
2017-01-31 03:26:17 +00:00
|
|
|
|
|
|
|
if (emitter) {
|
2017-04-06 06:11:13 +00:00
|
|
|
emitter.emit('after-reactdom-render', { Component, ErrorComponent })
|
2017-01-31 03:26:17 +00:00
|
|
|
}
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
2017-09-28 20:36:48 +00:00
|
|
|
|
|
|
|
let isInitialRender = true
|
|
|
|
function renderReactElement (reactEl, domEl) {
|
|
|
|
if (isInitialRender) {
|
|
|
|
ReactDOM.hydrate(reactEl, domEl)
|
|
|
|
isInitialRender = false
|
|
|
|
} else {
|
|
|
|
ReactDOM.render(reactEl, domEl)
|
|
|
|
}
|
|
|
|
}
|