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'
|
2018-08-30 12:02:18 +00:00
|
|
|
import Loadable from '../lib/loadable'
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2017-03-04 16:48:33 +00:00
|
|
|
// Polyfill Promise globally
|
2018-07-24 09:24:40 +00:00
|
|
|
// This is needed because Webpack's dynamic loading(common chunks) code
|
2017-03-04 16:48:33 +00:00
|
|
|
// depends on Promise.
|
|
|
|
// So, we need to polyfill it.
|
2018-08-23 23:57:04 +00:00
|
|
|
// See: https://webpack.js.org/guides/code-splitting/#dynamic-imports
|
2017-03-04 16:48:33 +00:00
|
|
|
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,
|
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-07-24 09:24:40 +00:00
|
|
|
const prefix = assetPrefix || ''
|
|
|
|
|
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
|
2018-07-24 09:24:40 +00:00
|
|
|
__webpack_public_path__ = `${prefix}/_next/` //eslint-disable-line
|
2018-02-02 20:09:24 +00:00
|
|
|
// Initialize next/asset with the assetPrefix
|
2018-07-24 09:24:40 +00:00
|
|
|
asset.setAssetPrefix(prefix)
|
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()
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
const pageLoader = new PageLoader(buildId, prefix)
|
2017-04-11 14:33:18 +00:00
|
|
|
window.__NEXT_LOADED_PAGES__.forEach(({ route, fn }) => {
|
|
|
|
pageLoader.registerPage(route, fn)
|
|
|
|
})
|
|
|
|
delete window.__NEXT_LOADED_PAGES__
|
|
|
|
window.__NEXT_REGISTER_PAGE = pageLoader.registerPage.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
|
2018-07-24 09:24:40 +00:00
|
|
|
let webpackHMR
|
2017-04-06 06:11:13 +00:00
|
|
|
export let router
|
|
|
|
export let ErrorComponent
|
|
|
|
let Component
|
2018-04-12 08:33:22 +00:00
|
|
|
let App
|
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-07-24 09:24:40 +00:00
|
|
|
webpackHMR: passedWebpackHMR
|
2018-04-18 16:18:06 +00:00
|
|
|
} = {}) => {
|
2018-07-24 09:24:40 +00:00
|
|
|
// This makes sure this specific line is removed in production
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
webpackHMR = passedWebpackHMR
|
2017-04-17 20:15:50 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
await Loadable.preloadReady()
|
|
|
|
|
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) {
|
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-07-24 09:24:40 +00:00
|
|
|
const {App, err} = props
|
2018-04-18 16:18:06 +00:00
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2018-07-24 09:24:40 +00:00
|
|
|
throw webpackHMR.prepareError(err)
|
2018-04-18 16:18:06 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
// Make sure we log the error to the console, otherwise users can't track down issues.
|
|
|
|
console.error(err)
|
2018-04-18 16:18:06 +00:00
|
|
|
|
|
|
|
// In production we do a normal render with the `ErrorComponent` as component.
|
2018-07-11 21:58:42 +00:00
|
|
|
// If we've gotten here upon initial render, we can use the props from the server.
|
|
|
|
// Otherwise, we need to call `getInitialProps` on `App` before mounting.
|
|
|
|
const initProps = props.props
|
|
|
|
? props.props
|
|
|
|
: await loadGetInitialProps(App, {Component: ErrorComponent, router, ctx: {err, pathname, query, asPath}})
|
|
|
|
|
|
|
|
await doRender({...props, err, Component: ErrorComponent, props: initProps})
|
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-07-24 09:24:40 +00:00
|
|
|
// In development runtime errors are caught by react-error-overlay.
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
renderReactElement((
|
|
|
|
<App {...appProps} />
|
|
|
|
), appContainer)
|
|
|
|
} else {
|
|
|
|
// In production we catch runtime errors using componentDidCatch which will trigger renderError.
|
|
|
|
const onError = async (error) => {
|
2018-05-31 09:47:29 +00:00
|
|
|
try {
|
2018-07-24 09:24:40 +00:00
|
|
|
await renderError({App, err: error})
|
2018-05-31 09:47:29 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error('Error while rendering error page: ', err)
|
|
|
|
}
|
|
|
|
}
|
2018-07-24 09:24:40 +00:00
|
|
|
renderReactElement((
|
|
|
|
<ErrorBoundary onError={onError}>
|
|
|
|
<App {...appProps} />
|
|
|
|
</ErrorBoundary>
|
|
|
|
), appContainer)
|
2018-04-18 16:18:06 +00:00
|
|
|
}
|
2017-01-31 03:26:17 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|