Skip to content

Commit bf7f1bf

Browse files
Merge pull request #120 from EdonGashi/patch-1
Added preloadWeak static method
2 parents 4fae5eb + 3752c46 commit bf7f1bf

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,19 @@ const MyUniversalComponent = universal(import('./MyComponent'))
274274
275275
// call this only after you're sure it has loaded
276276
MyUniversalComponent.doSomething()
277+
278+
// If you are not sure if the component has loaded or rendered, call preloadWeak().
279+
// This will attempt to hoist and return the inner component,
280+
// but only if it can be loaded synchronously, otherwise null will be returned.
281+
const InnerComponent = MyUniversalComponent.preloadWeak()
282+
if (InnerComponent) {
283+
InnerComponent.doSomething()
284+
}
277285
```
278286
> NOTE: for imports using dynamic expressions, conflicting methods will be overwritten by the current component
279287

288+
> NOTE: preloadWeak() will not cause network requests, which means that if the component has not loaded, it will return null. Use it only when you need to retrieve and hoist the wrapped component before rendering. Calling preloadWeak() on your server will ensure that all statics are hoisted properly.
289+
280290
## Props API
281291

282292
- `isLoading: boolean`

src/index.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,29 @@ export default function universal<Props: Props>(
7676
return requireAsync(props, context)
7777
})
7878
.then(Component => {
79-
hoist(UniversalComponent, Component, { preload: true })
79+
hoist(UniversalComponent, Component, {
80+
preload: true,
81+
preloadWeak: true
82+
})
8083
return Component
8184
})
8285
}
8386

87+
static preloadWeak(props: Props, context: Object = {}) {
88+
props = props || {}
89+
const { requireSync } = req(component, options, props)
90+
91+
const Component = requireSync(props, context)
92+
if (Component) {
93+
hoist(UniversalComponent, Component, {
94+
preload: true,
95+
preloadWeak: true
96+
})
97+
}
98+
99+
return Component
100+
}
101+
84102
static contextTypes = {
85103
store: PropTypes.object,
86104
report: PropTypes.func
@@ -228,7 +246,10 @@ export default function universal<Props: Props>(
228246
const { Component, error } = state
229247

230248
if (Component && !error) {
231-
hoist(UniversalComponent, Component, { preload: true })
249+
hoist(UniversalComponent, Component, {
250+
preload: true,
251+
preloadWeak: true
252+
})
232253

233254
if (this.props.onAfter) {
234255
const { onAfter } = this.props

0 commit comments

Comments
 (0)