Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(component): add preload method #224

Merged
merged 1 commit into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/component/src/createLoadable.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ function createLoadable({ resolve = identity, render, onLoad }) {
<EnhancedInnerLoadable forwardedRef={ref} {...props} />
))

Loadable.preload = props => {
if (typeof window === 'undefined') {
throw new Error('`preload` cannot be called server-side')
}
ctor.requireAsync(props)
}

return Loadable
}

Expand Down
3 changes: 1 addition & 2 deletions packages/component/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import hoistNonReactStatics from 'hoist-non-react-statics'
export function resolveComponent(loadedModule, { Loadable }) {
const Component = loadedModule.default || loadedModule
hoistNonReactStatics(Loadable, Component, {
prefetch: true,
Prefetch: true,
preload: true,
})
return Component
}
20 changes: 18 additions & 2 deletions website/src/pages/docs/api-loadable-component.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ order: 10

# @loadable/component

## Loadable
## loadable

Create a loadable component.

Expand All @@ -23,7 +23,7 @@ import loadable from '@loadable/component'
const OtherComponent = loadable(() => import('./OtherComponent'))
```

## Lazy
## lazy

Create a loadable component "Suspense" ready.

Expand All @@ -46,6 +46,22 @@ A component created using `loadable` or `lazy`.
| `fallback` | Fallback displayed during the loading. |
| `...` | Props are forwarded as first argument of `loadFn` |

## LoadableComponent.preload

Force the loading of a component.

| Arguments | Description |
| --------- | ---------------------------------- |
| `args` | Props passed to the load function. |

```js
import loadable from '@loadable/component'

const OtherComponent = loadable(() => import('./OtherComponent'))

OtherComponent.preload()
```

## loadable.lib

Create a loadable library.
Expand Down
34 changes: 33 additions & 1 deletion website/src/pages/docs/prefetching.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,40 @@ Most of the time, you want to "prefetch" a component, it means it will be loaded
import loadable from '@loadable/component'

const OtherComponent = loadable(() =>
import(/* webpackPrefetch: true */ './OtherComponent')
import(/* webpackPrefetch: true */ './OtherComponent'),
)
```

> You can extract prefetched resources server-side to add `<link rel="prefetch">` in your head.

## Manually preload a component

It is possible to _force_ the preload of a component. It has the same effect as if the component is rendered for the first time.

It can be useful to trigger a `preload` on mouse over:

```js
import loadable from '@loadable/component'

const Infos = () => loadable(() => './Infos')

function App() {
const [show, setShow] = useState(false)
return (
<div>
<a onMouseOver={() => Infos.preload()} onClick={() => setShow(true)}>
Show Infos
</a>
{show && <Infos />}
</div>
)
}

const OtherComponent = loadable(() =>
import(/* webpackPrefetch: true */ './OtherComponent'),
)
```

> `preload` is not available server-side, you should only call it client-side. If you want to use prefetching server-side, use webpack hints instead.

> `preload` is aggressive and doesn't take care of network condition and data saving preference of the user. You should call it carefully.