-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
hydrate.js
40 lines (38 loc) · 1.02 KB
/
hydrate.js
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
import { createElement as h, startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
/**
* @see https://caniuse.com/requestidlecallback
*/
const requestIdleCallbackUltra = (typeof self !== "undefined" &&
self.requestIdleCallback &&
self.requestIdleCallback.bind(window)) ||
function (cb) {
const start = Date.now();
return setTimeout(function () {
cb({
didTimeout: false,
timeRemaining() {
return Math.max(0, 50 - (Date.now() - start));
},
});
}, 1);
};
/**
* @param {Element | Document} container
* @param {React.ReactNode} element
* @param {import('react-dom/client').HydrationOptions} [options]
*/
export default function hydrate(container, element, options) {
requestIdleCallbackUltra(() => {
/**
* @see https://reactjs.org/docs/react-api.html#starttransition
*/
startTransition(() => {
hydrateRoot(
container,
h(StrictMode, { children: element }),
options,
);
});
});
}