-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
use-server-handoff-complete.ts
60 lines (49 loc) · 1.74 KB
/
use-server-handoff-complete.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as React from 'react'
import { env } from '../utils/env'
/**
* This is used to determine if we're hydrating in React 18.
*
* The `useServerHandoffComplete` hook doesn't work with `<Suspense>`
* because it assumes all hydration happens at one time during page load.
*
* Given that the problem only exists in React 18 we can rely
* on newer APIs to determine if hydration is happening.
*/
function useIsHydratingInReact18(): boolean {
let isServer = typeof document === 'undefined'
// React < 18 doesn't have any way to figure this out afaik
if (!('useSyncExternalStore' in React)) {
return false
}
// This weird pattern makes sure bundlers don't throw at build time
// because `useSyncExternalStore` isn't defined in React < 18
const useSyncExternalStore = ((r) => r.useSyncExternalStore)(React)
// @ts-ignore
let result = useSyncExternalStore(
() => () => {},
() => false,
() => (isServer ? false : true)
)
return result
}
// TODO: We want to get rid of this hook eventually
export function useServerHandoffComplete() {
let isHydrating = useIsHydratingInReact18()
let [complete, setComplete] = React.useState(env.isHandoffComplete)
if (complete && env.isHandoffComplete === false) {
// This means we are in a test environment and we need to reset the handoff state
// This kinda breaks the rules of React but this is only used for testing purposes
// And should theoretically be fine
setComplete(false)
}
React.useEffect(() => {
if (complete === true) return
setComplete(true)
}, [complete])
// Transition from pending to complete (forcing a re-render when server rendering)
React.useEffect(() => env.handoff(), [])
if (isHydrating) {
return false
}
return complete
}