Skip to content

Commit 6454eed

Browse files
chore(docs): Move loadable-components instructions (#35116)
Co-authored-by: Lennart <[email protected]>
1 parent bff56ac commit 6454eed

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

docs/docs/using-client-side-only-packages.md

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ On occasion, you may need to use a function or library that only works client-si
77
You'll need to use one of the workarounds outlined below if your project fails to compile with `gatsby develop` or `gatsby build` with an error like:
88

99
```shell
10-
Reference error: Window is not Defined
10+
Reference error: window is not defined
1111
```
1212

1313
## Workaround 1: Use a different library or approach
@@ -21,7 +21,7 @@ In the component where you need it, load the package via CDN using a [`<script /
2121
To embed your script, you can:
2222

2323
- Include it in a custom component as needed using [`react-helmet`](https://github.com/nfl/react-helmet).
24-
- Add the script tag directly in your base HTML using Gatsby's [html.js](/docs/custom-html/)
24+
- Add the script tag directly by using Gatsby's [`setHeadComponents`](/docs/reference/config-files/gatsby-ssr/#onRenderBody) in the `onRenderBody` API in `gatsby-ssr`.
2525

2626
You should then follow React's guidelines for [Integrating with DOM Manipulation Plugins](https://reactjs.org/docs/integrating-with-other-libraries.html#integrating-with-dom-manipulation-plugins), using the methods available in the [React Component Lifecycle](https://reactjs.org/docs/react-component.html#the-component-lifecycle) to interact with the library you're using.
2727

@@ -50,16 +50,41 @@ class MyComponent extends Component {
5050
}
5151
```
5252

53-
## Workaround 3: Load client-side dependent components with loadable-components
53+
## Workaround 3: Use React.lazy and Suspense on client-side only
54+
55+
React.lazy and Suspense are not ready for server-side rendering, but they can be used by checking that the code is executed only on the client. While this solution is inferior to `loadable-components`, that works both on server side and client, it still provides an alternative for dealing with client-side only packages, without an added dependency. Remember that the following code could break if executed without the `isSSR` guard.
56+
57+
```jsx
58+
import React from "react"
59+
60+
const ClientSideOnlyLazy = React.lazy(() =>
61+
import("../components/ClientSideOnly")
62+
)
63+
const MyPage = () => {
64+
const isSSR = typeof window === "undefined"
65+
66+
return (
67+
<>
68+
{!isSSR && (
69+
<React.Suspense fallback={<div />}>
70+
<ClientSideOnlyLazy />
71+
</React.Suspense>
72+
)}
73+
</>
74+
)
75+
}
76+
```
77+
78+
## Workaround 4: Load client-side dependent components with loadable-components
5479

5580
Install [loadable-components](https://github.com/smooth-code/loadable-components) and use it as a wrapper for a component that wants to use a client-side only package.
5681

5782
```shell
5883
npm install @loadable/component
59-
# or use yarn
60-
yarn add @loadable/component
6184
```
6285

86+
And in your component:
87+
6388
```jsx
6489
import React, { Component } from "react"
6590
import PropTypes from "prop-types"
@@ -80,33 +105,6 @@ const LoadableBuyButton = Loadable(() => import("./ShopifyBuyButton"))
80105
export default LoadableBuyButton
81106
```
82107

83-
## Workaround 4: Use React.lazy and Suspense on client-side only
84-
85-
React.lazy and Suspense are still not ready for server-side rendering, but they can still be used by checking that the code is executed only on the client.
86-
While this solution is inferior to `loadable-components`, that works both on server side and client, it still provides an alternative for dealing with client-side only packages, without an added dependency.
87-
Remember that the following code could break if executed without the `isSSR` guard.
88-
89-
```jsx
90-
import React from "react"
91-
92-
const ClientSideOnlyLazy = React.lazy(() =>
93-
import("../components/ClientSideOnly")
94-
)
95-
const MyPage = () => {
96-
const isSSR = typeof window === "undefined"
97-
98-
return (
99-
<>
100-
{!isSSR && (
101-
<React.Suspense fallback={<div />}>
102-
<ClientSideOnlyLazy />
103-
</React.Suspense>
104-
)}
105-
</>
106-
)
107-
}
108-
```
109-
110108
> **Note:** There are other potential workarounds than those listed here. If you've had success with another method, check out the [contributing docs](/contributing/docs-contributions/) and add yours!
111109
112110
If all else fails, you may also want to check out the documentation on [Debugging HTML Builds](/docs/debugging-html-builds/).

packages/gatsby/src/utils/api-node-docs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export const preprocessSource = true
371371
export const onCreateBabelConfig = true
372372

373373
/**
374-
* Let plugins extend/mutate the site's webpack configuration.
374+
* Let plugins extend/mutate the site's webpack configuration. This method can be used by any Gatsby site, app, or plugin, not just plugins.
375375
*
376376
* See also the documentation for [`setWebpackConfig`](/docs/actions/#setWebpackConfig).
377377
*

0 commit comments

Comments
 (0)