Skip to content

Commit 63cc8fa

Browse files
authored
fix(docs): Copy edits for debugging html doc + add React-specific example (#30745)
1 parent eed1d43 commit 63cc8fa

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

docs/docs/debugging-html-builds.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
title: Debugging HTML Builds
33
---
44

5-
Errors while building static HTML files generally happen for one of the following reasons:
6-
7-
1. Some of your code references "browser globals" like `window` or `document`. If
8-
this is your problem you should see an error above like "window is not
9-
defined". To fix this, find the offending code and either a) check before
10-
calling the code if window is defined so the code doesn't run while Gatsby is
11-
building (see code sample below) or b) if the code is in the render function
12-
of a React.js component, move that code into a [`componentDidMount` lifecycle](https://reactjs.org/docs/react-component.html#componentdidmount) or into a [`useEffect` hook](https://reactjs.org/docs/hooks-reference.html#useeffect), which
5+
Errors while building static HTML files (The build-time React SSR process) generally happen for one of the following reasons:
6+
7+
1. Some of your code references "browser globals" like `window` or `document`
8+
that aren't available in Node.js. If this is your problem you should see an
9+
error above like "window is not defined". To fix this, find the offending
10+
code and either a) check before calling the code if window is defined so the
11+
code doesn't run while Gatsby is building (see code sample below) or b) if
12+
the code is in the render function of a React.js component, move that code
13+
into a [`componentDidMount`
14+
lifecycle](https://reactjs.org/docs/react-component.html#componentdidmount)
15+
or into a [`useEffect`
16+
hook](https://reactjs.org/docs/hooks-reference.html#useeffect), which
1317
ensures the code doesn't run unless it's in the browser.
1418

1519
2. Check that each of your JS files listed in your `pages` directory (and any
@@ -22,18 +26,41 @@ Errors while building static HTML files generally happen for one of the followin
2226
is stricter than v3](/docs/reference/release-notes/migrating-from-v1-to-v2/#convert-to-either-pure-commonjs-or-pure-es6).
2327
The solution is to only use `import` and this also extends to `gatsby-ssr` and `gatsby-browser` files.
2428

25-
4. Your app is not correctly [hydrated](https://reactjs.org/docs/react-dom.html), which results in gatsby develop and gatsby
26-
build being inconsistent. It's possible that a change in a file like `gatsby-ssr` or `gatsby-browser` has a structure that is
27-
not reflected in the other file, meaning that there is a mismatch between client and server output.
29+
4. Your app doesn't correctly
30+
[hydrate](https://reactjs.org/docs/react-dom.html) in the client, which
31+
results in gatsby develop and gatsby build being inconsistent. It's possible
32+
that a change in a file like `gatsby-ssr` or `gatsby-browser` has
33+
a structure that is not reflected in the other file, meaning that there is
34+
a mismatch between client and server output.
2835

2936
5. Some other reason :-) #1 is the most common reason building static files
3037
fail. If it's another reason, you have to be a bit more creative in figuring
3138
out the problem.
3239

3340
## How to check if `window` is defined
3441

42+
When referencing `window` in a React component.
43+
44+
```jsx
45+
import * as React from "react"
46+
47+
// Check if window is defined (so if in the browser or in node.js).
48+
const isBrowser = typeof window !== "undefined"
49+
50+
export default function MyComponent() {
51+
let loggedIn = false
52+
if (isBrowser) {
53+
window.localstorage.getItem("isLoggedIn") === "true"
54+
}
55+
56+
return <div>Am I logged in? {loggedIn}</div>
57+
}
58+
```
59+
60+
When requiring a module:
61+
3562
```javascript
36-
// Requiring function causes error during builds
63+
// Requiring a function causes an error during builds
3764
// as the code tries to reference window
3865
const module = require("module") // Error
3966

0 commit comments

Comments
 (0)