Skip to content

Commit 35f81a6

Browse files
committed
fix: fix loadComponents without valid state
Fix #34
1 parent 8076cb3 commit 35f81a6

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/loadComponents.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { LOADABLE, LOADABLE_STATE } from './constants'
33
import * as componentTracker from './componentTracker'
44

55
function loadState(rootState) {
6-
if (!rootState.children) return null
6+
if (!rootState.children) return Promise.resolve(null)
77

88
return Promise.all(
99
rootState.children.map(state => {
@@ -50,7 +50,7 @@ function loadComponents() {
5050
}
5151

5252
const state = window[LOADABLE_STATE]
53-
if (!state) {
53+
if (!state || !state.children) {
5454
throw new Error(
5555
'loadable-components state not found. ' +
5656
'You have a problem server-side. ' +

src/loadComponents.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,26 @@ describe('loadComponents', () => {
3030
expect(Component2.load).toHaveBeenCalled()
3131
expect(Component3.load).toHaveBeenCalled()
3232
})
33+
34+
it('should handle no LOADABLE_STATE', async () => {
35+
delete window[LOADABLE_STATE]
36+
expect.assertions(1)
37+
38+
try {
39+
await loadComponents()
40+
} catch (err) {
41+
expect(err.message).toMatch(/loadable-components state not found/)
42+
}
43+
})
44+
45+
it('should handle no children', async () => {
46+
delete window[LOADABLE_STATE].children
47+
expect.assertions(1)
48+
49+
try {
50+
await loadComponents()
51+
} catch (err) {
52+
expect(err.message).toMatch(/loadable-components state not found/)
53+
}
54+
})
3355
})

0 commit comments

Comments
 (0)