You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/using-client-side-only-packages.md
+30-32Lines changed: 30 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ On occasion, you may need to use a function or library that only works client-si
7
7
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:
8
8
9
9
```shell
10
-
Reference error: Window is not Defined
10
+
Reference error: window is not defined
11
11
```
12
12
13
13
## 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 /
21
21
To embed your script, you can:
22
22
23
23
- 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`.
25
25
26
26
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.
27
27
@@ -50,16 +50,41 @@ class MyComponent extends Component {
50
50
}
51
51
```
52
52
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
+
importReactfrom"react"
59
+
60
+
constClientSideOnlyLazy=React.lazy(() =>
61
+
import("../components/ClientSideOnly")
62
+
)
63
+
constMyPage= () => {
64
+
constisSSR=typeofwindow==="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
54
79
55
80
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.
## 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
-
importReactfrom"react"
91
-
92
-
constClientSideOnlyLazy=React.lazy(() =>
93
-
import("../components/ClientSideOnly")
94
-
)
95
-
constMyPage= () => {
96
-
constisSSR=typeofwindow==="undefined"
97
-
98
-
return (
99
-
<>
100
-
{!isSSR && (
101
-
<React.Suspense fallback={<div />}>
102
-
<ClientSideOnlyLazy />
103
-
</React.Suspense>
104
-
)}
105
-
</>
106
-
)
107
-
}
108
-
```
109
-
110
108
> **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!
111
109
112
110
If all else fails, you may also want to check out the documentation on [Debugging HTML Builds](/docs/debugging-html-builds/).
0 commit comments