2018-04-18 16:18:06 +00:00
|
|
|
import React from 'react'
|
2017-01-12 01:58:20 +00:00
|
|
|
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-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'
|
2018-01-30 15:40:52 +00:00
|
|
|
import * as asset from '../lib/asset'
|
2018-02-26 11:03:27 +00:00
|
|
|
import * as envConfig from '../lib/runtime-config'
|
2018-05-31 09:47:29 +00:00
|
|
|
import ErrorBoundary from './error-boundary'
|
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,
|
2018-02-13 15:13:22 +00:00
|
|
|
page,
|
2017-01-12 01:58:20 +00:00
|
|
|
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,
|
2018-02-26 11:03:27 +00:00
|
|
|
assetPrefix,
|
|
|
|
runtimeConfig
|
2017-02-28 17:31:17 +00:00
|
|
|
},
|
|
|
|
location
|
2017-01-12 01:58:20 +00:00
|
|
|
} = window
|
|
|
|
|
2018-02-02 20:09:24 +00:00
|
|
|
// With dynamic assetPrefix it's no longer possible to set assetPrefix at the build time
|
|
|
|
// So, this is how we do it in the client side at runtime
|
|
|
|
__webpack_public_path__ = `${assetPrefix}/_next/webpack/` //eslint-disable-line
|
|
|
|
// Initialize next/asset with the assetPrefix
|
2018-01-30 15:40:52 +00:00
|
|
|
asset.setAssetPrefix(assetPrefix)
|
2018-02-26 11:03:27 +00:00
|
|
|
// Initialize next/config with the environment configuration
|
2018-02-27 16:50:14 +00:00
|
|
|
envConfig.setConfig({
|
|
|
|
serverRuntimeConfig: {},
|
|
|
|
publicRuntimeConfig: runtimeConfig
|
|
|
|
})
|
2018-01-30 15:40:52 +00:00
|
|
|
|
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
|
2018-05-31 09:47:29 +00:00
|
|
|
let DevErrorOverlay
|
2017-04-06 06:11:13 +00:00
|
|
|
let Component
|
2018-04-12 08:33:22 +00:00
|
|
|
let App
|
2017-10-21 10:58:59 +00:00
|
|
|
let stripAnsi = (s) => s
|
2018-04-18 16:18:06 +00:00
|
|
|
let applySourcemaps = (e) => e
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2017-10-30 15:01:40 +00:00
|
|
|
export const emitter = new EventEmitter()
|
|
|
|
|
2018-04-18 16:18:06 +00:00
|
|
|
export default async ({
|
2018-05-31 09:47:29 +00:00
|
|
|
DevErrorOverlay: passedDevErrorOverlay,
|
2018-04-18 16:18:06 +00:00
|
|
|
stripAnsi: passedStripAnsi,
|
|
|
|
applySourcemaps: passedApplySourcemaps
|
|
|
|
} = {}) => {
|
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
|
2018-04-18 16:18:06 +00:00
|
|
|
applySourcemaps = passedApplySourcemaps || applySourcemaps
|
2018-05-31 09:47:29 +00:00
|
|
|
DevErrorOverlay = passedDevErrorOverlay
|
2017-04-06 06:11:13 +00:00
|
|
|
ErrorComponent = await pageLoader.loadPage('/_error')
|
2018-04-12 08:33:22 +00:00
|
|
|
App = await pageLoader.loadPage('/_app')
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2018-04-18 16:18:06 +00:00
|
|
|
let initialErr = err
|
|
|
|
|
2017-04-06 06:11:13 +00:00
|
|
|
try {
|
2018-02-13 15:13:22 +00:00
|
|
|
Component = await pageLoader.loadPage(page)
|
2018-04-12 08:33:22 +00:00
|
|
|
|
|
|
|
if (typeof Component !== 'function') {
|
|
|
|
throw new Error(`The default export is not a React Component in page: "${pathname}"`)
|
|
|
|
}
|
2018-04-18 16:18:06 +00:00
|
|
|
} catch (error) {
|
|
|
|
// This catches errors like throwing in the top level of a module
|
|
|
|
initialErr = error
|
2017-04-06 06:11:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 16:40:09 +00:00
|
|
|
router = createRouter(pathname, query, asPath, {
|
2018-04-12 08:33:22 +00:00
|
|
|
initialProps: props,
|
2017-04-06 06:11:13 +00:00
|
|
|
pageLoader,
|
2018-04-12 08:33:22 +00:00
|
|
|
App,
|
2017-04-06 06:11:13 +00:00
|
|
|
Component,
|
|
|
|
ErrorComponent,
|
2018-04-18 16:18:06 +00:00
|
|
|
err: initialErr
|
2017-04-06 06:11:13 +00:00
|
|
|
})
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2018-05-31 09:47:29 +00:00
|
|
|
router.subscribe(({ App, Component, props, hash, err }) => {
|
|
|
|
render({ App, 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)
|
2018-05-31 09:47:29 +00:00
|
|
|
render({ App, Component, props, hash, err: initialErr, 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) {
|
2018-01-30 15:40:52 +00:00
|
|
|
if (props.err) {
|
2018-04-18 16:18:06 +00:00
|
|
|
await renderError(props)
|
2017-04-01 21:03:40 +00:00
|
|
|
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
|
2018-04-18 16:18:06 +00:00
|
|
|
await renderError({...props, 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.
|
2018-04-18 16:18:06 +00:00
|
|
|
export async function renderError (props) {
|
2018-05-31 09:47:29 +00:00
|
|
|
const {err, errorInfo} = props
|
2018-04-18 16:18:06 +00:00
|
|
|
|
|
|
|
// In development we apply sourcemaps to the error
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
await applySourcemaps(err)
|
|
|
|
}
|
|
|
|
|
2018-05-31 09:47:29 +00:00
|
|
|
const str = stripAnsi(`${err.message}\n${err.stack}${errorInfo ? `\n\n${errorInfo.componentStack}` : ''}`)
|
2018-04-18 16:18:06 +00:00
|
|
|
console.error(str)
|
|
|
|
|
|
|
|
if (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)
|
2018-05-31 09:47:29 +00:00
|
|
|
renderReactElement(<DevErrorOverlay error={err} />, errorContainer)
|
2018-04-18 16:18:06 +00:00
|
|
|
return
|
2017-04-01 21:03:40 +00:00
|
|
|
}
|
2018-04-18 16:18:06 +00:00
|
|
|
|
|
|
|
// In production we do a normal render with the `ErrorComponent` as component.
|
|
|
|
// `App` will handle the calling of `getInitialProps`, which will include the `err` on the context
|
|
|
|
await doRender({...props, err, Component: ErrorComponent})
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 09:47:29 +00:00
|
|
|
async function doRender ({ App, Component, props, hash, err, emitter: emitterProp = emitter }) {
|
2018-04-12 08:33:22 +00:00
|
|
|
// Usual getInitialProps fetching is handled in next/router
|
|
|
|
// this is for when ErrorComponent gets replaced by Component by HMR
|
2017-01-12 01:58:20 +00:00
|
|
|
if (!props && Component &&
|
|
|
|
Component !== ErrorComponent &&
|
|
|
|
lastAppProps.Component === ErrorComponent) {
|
2017-05-03 16:40:09 +00:00
|
|
|
const { pathname, query, asPath } = router
|
2018-04-12 08:33:22 +00:00
|
|
|
props = await loadGetInitialProps(App, {Component, router, ctx: {err, pathname, query, asPath}})
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Component = Component || lastAppProps.Component
|
|
|
|
props = props || lastAppProps.props
|
|
|
|
|
2018-04-12 08:33:22 +00:00
|
|
|
const appProps = { Component, hash, err, router, headManager, ...props }
|
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-10-30 15:01:40 +00:00
|
|
|
emitterProp.emit('before-reactdom-render', { Component, ErrorComponent, appProps })
|
|
|
|
|
2017-04-01 21:03:40 +00:00
|
|
|
// We need to clear any existing runtime error messages
|
|
|
|
ReactDOM.unmountComponentAtNode(errorContainer)
|
2018-04-18 16:18:06 +00:00
|
|
|
|
2018-05-31 09:47:29 +00:00
|
|
|
let onError = null
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'development') {
|
|
|
|
onError = async (error, errorInfo) => {
|
|
|
|
try {
|
|
|
|
await renderError({App, err: error, errorInfo})
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error while rendering error page: ', err)
|
|
|
|
}
|
|
|
|
}
|
2018-04-18 16:18:06 +00:00
|
|
|
}
|
2017-01-31 03:26:17 +00:00
|
|
|
|
2018-05-31 09:47:29 +00:00
|
|
|
// In development we render a wrapper component that catches runtime errors.
|
|
|
|
renderReactElement((
|
|
|
|
<ErrorBoundary ErrorReporter={DevErrorOverlay} onError={onError}>
|
|
|
|
<App {...appProps} />
|
|
|
|
</ErrorBoundary>
|
|
|
|
), appContainer)
|
|
|
|
|
2017-10-30 15:01:40 +00:00
|
|
|
emitterProp.emit('after-reactdom-render', { Component, ErrorComponent, appProps })
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
2017-09-28 20:36:48 +00:00
|
|
|
|
|
|
|
let isInitialRender = true
|
|
|
|
function renderReactElement (reactEl, domEl) {
|
2018-01-30 15:40:52 +00:00
|
|
|
// The check for `.hydrate` is there to support React alternatives like preact
|
|
|
|
if (isInitialRender && typeof ReactDOM.hydrate === 'function') {
|
2017-09-28 20:36:48 +00:00
|
|
|
ReactDOM.hydrate(reactEl, domEl)
|
|
|
|
isInitialRender = false
|
|
|
|
} else {
|
|
|
|
ReactDOM.render(reactEl, domEl)
|
|
|
|
}
|
|
|
|
}
|