-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hydrate.mjs
78 lines (67 loc) · 2.37 KB
/
hydrate.mjs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// @ts-check
import Cache from "graphql-react/Cache.mjs";
import Loading from "graphql-react/Loading.mjs";
import { createElement as h } from "react";
import { hydrateRoot } from "react-dom/client";
import ClientProvider from "./ClientProvider.mjs";
import createPseudoNode from "./createPseudoNode.mjs";
import HeadContent from "./HeadContent.mjs";
import HeadManager from "./HeadManager.mjs";
/**
* Hydrates the Ruck document head and body React apps after SSR.
* @param {object} options Options.
* @param {import("./serve.mjs").Router} options.router Router.
* @param {import("./serve.mjs").AppComponent} options.appComponent App React
* component.
* @param {Record<string, any>} options.cacheData Cache data.
*/
export default async function hydrate({ router, appComponent, cacheData }) {
if (typeof router !== "function") {
throw new TypeError("Option `router` must be a function.");
}
const bodyReactRootContainer = document.getElementById("ruck-app");
if (!bodyReactRootContainer) {
throw new Error("Ruck body React app DOM node missing.");
}
const ruckHeadStart = document.head.querySelector('[name="ruck-head-start"]');
if (!ruckHeadStart) {
throw new Error("Ruck head React app start DOM node missing.");
}
const ruckHeadEnd = document.head.querySelector('[name="ruck-head-end"]');
if (!ruckHeadEnd) {
throw new Error("Ruck head React app end DOM node missing.");
}
const headReactRootContainer = /** @type {HTMLHeadElement} */ (
createPseudoNode(ruckHeadStart, ruckHeadEnd)
);
const headManager = new HeadManager();
const initialRouteUrl = new URL(location.href);
// Todo: Validate what the router returns.
const { content, cleanup } = router(initialRouteUrl, headManager, true);
const initialRoute = {
url: initialRouteUrl,
content: await content,
cleanup,
};
// Hydrate the body React app first, so the head manager knows what content to
// render in the head app.
hydrateRoot(
bodyReactRootContainer,
h(
ClientProvider,
{
hydrationTimeStamp: performance.now(),
headManager,
cache: new Cache(cacheData),
loading: new Loading(),
router,
initialRoute,
onEffectsDone() {
// Hydrate the head React app.
hydrateRoot(headReactRootContainer, h(HeadContent, { headManager }));
},
},
h(appComponent),
),
);
}