forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (79 loc) · 2.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { createElement } from 'react'
import ReactDOM from 'react-dom'
import mitt from 'mitt'
import HeadManager from './head-manager'
import { createRouter } from '../lib/router'
import App from '../lib/app'
import evalScript from '../lib/eval-script'
import { loadGetInitialProps, getURL } from '../lib/utils'
// 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
}
const {
__NEXT_DATA__: {
component,
errorComponent,
props,
err,
pathname,
query
},
location
} = window
const Component = evalScript(component).default
const ErrorComponent = evalScript(errorComponent).default
let lastAppProps
export const router = createRouter(pathname, query, getURL(), {
Component,
ErrorComponent,
err
})
const headManager = new HeadManager()
const container = document.getElementById('__next')
export default (onError) => {
const emitter = mitt()
router.subscribe(({ Component, props, hash, err }) => {
render({ Component, props, err, hash, emitter }, onError)
})
const hash = location.hash.substring(1)
render({ Component, props, hash, err, emitter }, onError)
return emitter
}
export async function render (props, onError = renderErrorComponent) {
try {
await doRender(props)
} catch (err) {
await onError(err)
}
}
async function renderErrorComponent (err) {
const { pathname, query } = router
const props = await loadGetInitialProps(ErrorComponent, { err, pathname, query })
await doRender({ Component: ErrorComponent, props, err })
}
async function doRender ({ Component, props, hash, err, emitter }) {
if (!props && Component &&
Component !== ErrorComponent &&
lastAppProps.Component === ErrorComponent) {
// fetch props if ErrorComponent was replaced with a page component by HMR
const { pathname, query } = router
props = await loadGetInitialProps(Component, { err, pathname, query })
}
if (emitter) {
emitter.emit('before-reactdom-render', { Component })
}
Component = Component || lastAppProps.Component
props = props || lastAppProps.props
const appProps = { Component, props, hash, err, router, headManager }
// lastAppProps has to be set before ReactDom.render to account for ReactDom throwing an error.
lastAppProps = appProps
ReactDOM.render(createElement(App, appProps), container)
if (emitter) {
emitter.emit('after-reactdom-render', { Component })
}
}