Skip to content

Commit 4cab4f9

Browse files
authored
feat(server): add option to disable SSR (#223)
Closes #195
1 parent b94a950 commit 4cab4f9

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

examples/server-side-rendering/src/client/App.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const A = loadable(() => import('./letters/A'))
77
const B = loadable(() => import('./letters/B'))
88
const C = loadable(() => import(/* webpackPreload: true */ './letters/C'))
99
const D = loadable(() => import(/* webpackPrefetch: true */ './letters/D'))
10+
const E = loadable(() => import('./letters/E'), { ssr: false })
1011
const X = loadable(props => import(`./letters/${props.letter}`))
1112

1213
// We keep some references to prevent uglify remove
@@ -18,8 +19,13 @@ const Moment = loadable.lib(() => import('moment'))
1819
const App = () => (
1920
<div>
2021
<A />
22+
<br />
2123
<B />
24+
<br />
2225
<X letter="A" />
26+
<br />
27+
<E />
28+
<br />
2329
<Moment>{moment => moment().format('HH:mm')}</Moment>
2430
</div>
2531
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default () => 'E'

packages/component/src/createLoadable.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ function createLoadable({ resolve = identity, render, onLoad }) {
4040

4141
// Server-side
4242
if (props.__chunkExtractor) {
43+
// This module has been marked with no SSR
44+
if (options.ssr === false) {
45+
return
46+
}
47+
4348
// We run load function, we assume that it won't fail and that it
4449
// triggers a synchronous loading of the module
4550
ctor.requireAsync(props).catch(() => {})

website/src/pages/docs/api-loadable-component.mdx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ order: 10
1010

1111
Create a loadable component.
1212

13-
| Arguments | Description |
14-
| ------------------ | ---------------------------------------- |
15-
| `loadFn` | The function call to load the component. |
16-
| `options` | Optional options. |
17-
| `options.fallback` | Fallback displayed during the loading. |
13+
| Arguments | Description |
14+
| ------------------ | -------------------------------------------------------------------- |
15+
| `loadFn` | The function call to load the component. |
16+
| `options` | Optional options. |
17+
| `options.fallback` | Fallback displayed during the loading. |
18+
| `options.ssr` | If `false`, it will not be processed server-side. Default to `true`. |
1819

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

website/src/pages/docs/server-side-rendering.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,14 @@ const extractor = new ChunkExtractor({ statsFile })
188188
const html = renderToString(extractor.collectChunks(<YourApp />))
189189
const styleTags = extractor.getStyleTags() // or extractor.getStyleElements();
190190
```
191+
192+
## Disable SSR on a specific loadable
193+
194+
Disable SSR on a specific loadable component with `ssr: false`:
195+
196+
```js
197+
import loadable from '@loadable/component'
198+
199+
// This dynamic import will not be processed server-side
200+
const Other = loadable(() => import('./Other'), { ssr: false })
201+
```

0 commit comments

Comments
 (0)