@@ -89,6 +89,37 @@ function createLoadable({
89
89
return Component
90
90
}
91
91
92
+ const cachedLoad = props => {
93
+ const cacheKey = getCacheKey ( props )
94
+ let promise = cache [ cacheKey ]
95
+
96
+ if ( ! promise || promise . status === STATUS_REJECTED ) {
97
+ promise = ctor . requireAsync ( props )
98
+ promise . status = STATUS_PENDING
99
+
100
+ cache [ cacheKey ] = promise
101
+
102
+ promise . then (
103
+ ( ) => {
104
+ promise . status = STATUS_RESOLVED
105
+ } ,
106
+ error => {
107
+ console . error (
108
+ 'loadable-components: failed to asynchronously load component' ,
109
+ {
110
+ fileName : ctor . resolve ( props ) ,
111
+ chunkName : ctor . chunkName ( props ) ,
112
+ error : error ? error . message : error ,
113
+ } ,
114
+ )
115
+ promise . status = STATUS_REJECTED
116
+ } ,
117
+ )
118
+ }
119
+
120
+ return promise
121
+ }
122
+
92
123
class InnerLoadable extends React . Component {
93
124
static getDerivedStateFromProps ( props , state ) {
94
125
const cacheKey = getCacheKey ( props )
@@ -270,33 +301,7 @@ function createLoadable({
270
301
resolveAsync ( ) {
271
302
const { __chunkExtractor, forwardedRef, ...props } = this . props
272
303
273
- let promise = this . getCache ( )
274
-
275
- if ( ! promise ) {
276
- promise = ctor . requireAsync ( props )
277
- promise . status = STATUS_PENDING
278
-
279
- this . setCache ( promise )
280
-
281
- promise . then (
282
- ( ) => {
283
- promise . status = STATUS_RESOLVED
284
- } ,
285
- error => {
286
- console . error (
287
- 'loadable-components: failed to asynchronously load component' ,
288
- {
289
- fileName : ctor . resolve ( this . props ) ,
290
- chunkName : ctor . chunkName ( this . props ) ,
291
- error : error ? error . message : error ,
292
- } ,
293
- )
294
- promise . status = STATUS_REJECTED
295
- } ,
296
- )
297
- }
298
-
299
- return promise
304
+ return cachedLoad ( props )
300
305
}
301
306
302
307
render ( ) {
@@ -343,12 +348,11 @@ function createLoadable({
343
348
344
349
// In future, preload could use `<link rel="preload">`
345
350
Loadable . preload = props => {
346
- cache [ getCacheKey ( ) ] = ctor . requireAsync ( props ) ;
351
+ Loadable . load ( props )
347
352
}
348
353
349
354
Loadable . load = props => {
350
- cache [ getCacheKey ( ) ] = ctor . requireAsync ( props ) ;
351
- return cache [ getCacheKey ( ) ] ;
355
+ return cachedLoad ( props )
352
356
}
353
357
354
358
return Loadable
0 commit comments