-
-
Notifications
You must be signed in to change notification settings - Fork 381
/
loadComponents.js
64 lines (54 loc) · 1.68 KB
/
loadComponents.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
/* eslint-env browser */
import { LOADABLE, LOADABLE_STATE } from './constants'
import * as componentTracker from './componentTracker'
function loadState(rootState) {
if (!rootState.children) return null
return Promise.all(
rootState.children.map(state => {
const component = componentTracker.get(state.id)
if (!component) {
console.warn(
'loadable-component client modules:',
componentTracker.getAll(),
)
console.warn(
'loadable-component server modules:',
window[LOADABLE_STATE],
)
throw new Error(
`loadable-components: module "${
state.id
}" is not found, client and server modules are not sync. You are probably not using the same resolver on server and client.`,
)
}
const getLoadable = component[LOADABLE]
if (typeof getLoadable !== 'function') {
throw new Error(
`loadable-components: module "${
state.id
}" is not a laodable component, please verify your SSR setup`,
)
}
return getLoadable()
.load()
.then(() => loadState(state))
}),
)
}
function loadComponents() {
if (typeof window === 'undefined') {
throw new Error(
'loadable-components: `loadComponents` must be called client-side: `window` is undefined',
)
}
const state = window[LOADABLE_STATE]
if (!state) {
throw new Error(
'loadable-components state not found. ' +
'You have a problem server-side. ' +
'Please verify that you have called `loadableState.getScriptTag()` server-side.',
)
}
return loadState(state)
}
export default loadComponents