Skip to content

Commit c00032b

Browse files
committed
Guard against missing context during SSR in TreeControls
`wrapRootElement` is only registered in `gatsby-browser.js`, not `gatsby-ssr.js`, so during static HTML generation `useSkoHubContext()` returns the raw `defaultState` object instead of `{ data, updateState }`. That made `data` undefined at render time and broke SSR with `Cannot read properties of undefined (reading 'sortBy')` for flat schemes (where TreeControls now renders to host the sort selector). Optional-chain the `data` reads in TreeControls so the component renders harmlessly during SSR; the dropdown becomes interactive once the browser hydrates inside the ContextProvider. Verified with `npm run build` against the interactivityType fixture (the exact page that was failing in CI).
1 parent 45dcc9a commit c00032b

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

src/components/TreeControls.jsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ const TreeControls = ({ hasNesting = true }) => {
3535

3636
// Initialise sortBy from config on first render if it hasn't been set yet.
3737
useEffect(() => {
38-
if (data.sortBy == null) {
38+
if (data && data.sortBy == null) {
3939
updateState({ ...data, sortBy: config?.sortBy || "prefLabel" })
4040
}
41-
}, [data.sortBy, config?.sortBy])
41+
}, [data?.sortBy, config?.sortBy])
4242

43-
const currentSort = data.sortBy ?? config?.sortBy ?? "prefLabel"
43+
const currentSort = data?.sortBy ?? config?.sortBy ?? "prefLabel"
4444

4545
return (
4646
<div className="TreeControls" css={style}>
@@ -75,7 +75,10 @@ const TreeControls = ({ hasNesting = true }) => {
7575
<select
7676
aria-label="Sort entries by"
7777
value={currentSort}
78-
onChange={(e) => updateState({ ...data, sortBy: e.target.value })}
78+
onChange={(e) =>
79+
updateState &&
80+
updateState({ ...(data || {}), sortBy: e.target.value })
81+
}
7982
>
8083
<option value="prefLabel">Label</option>
8184
<option value="notation">Notation</option>

0 commit comments

Comments
 (0)