-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathRehydrationContext.tsx
93 lines (85 loc) · 2.9 KB
/
RehydrationContext.tsx
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { useApolloClient } from "@apollo/client";
import React from "react";
import { NextSSRInMemoryCache } from "./NextSSRInMemoryCache";
import { ServerInsertedHTMLContext } from "next/navigation";
import { RehydrationContextValue } from "./types";
import { registerDataTransport, transportDataToJS } from "./dataTransport";
const ApolloRehydrationContext = React.createContext<
RehydrationContextValue | undefined
>(undefined);
export const RehydrationContextProvider = ({
children,
}: React.PropsWithChildren) => {
const { cache } = useApolloClient();
const rehydrationContext = React.useRef<RehydrationContextValue>();
if (typeof window == "undefined") {
if (!rehydrationContext.current) {
rehydrationContext.current = buildApolloRehydrationContext();
}
if (cache instanceof NextSSRInMemoryCache) {
cache.setRehydrationContext(rehydrationContext.current);
} else {
throw new Error(
"When using Next SSR, you must use the `NextSSRInMemoryCache`"
);
}
} else {
registerDataTransport();
}
return (
<ApolloRehydrationContext.Provider value={rehydrationContext.current}>
{children}
</ApolloRehydrationContext.Provider>
);
};
export function useRehydrationContext(): RehydrationContextValue | undefined {
const rehydrationContext = React.useContext(ApolloRehydrationContext);
const insertHtml = React.useContext(ServerInsertedHTMLContext);
// help transpilers to omit this code in bundling
if (typeof window !== "undefined") return;
if (
insertHtml &&
rehydrationContext &&
!rehydrationContext.currentlyInjected
) {
rehydrationContext.currentlyInjected = true;
insertHtml(() => <rehydrationContext.RehydrateOnClient />);
}
return rehydrationContext;
}
function buildApolloRehydrationContext(): RehydrationContextValue {
const rehydrationContext: RehydrationContextValue = {
currentlyInjected: false,
transportValueData: {},
transportedValues: {},
incomingResults: [],
RehydrateOnClient() {
rehydrationContext.currentlyInjected = false;
if (
!Object.keys(rehydrationContext.transportValueData).length &&
!Object.keys(rehydrationContext.incomingResults).length
)
return <></>;
console.log("transporting data", rehydrationContext.transportValueData);
console.log("transporting results", rehydrationContext.incomingResults);
const __html = transportDataToJS({
rehydrate: rehydrationContext.transportValueData,
results: rehydrationContext.incomingResults,
});
Object.assign(
rehydrationContext.transportedValues,
rehydrationContext.transportValueData
);
rehydrationContext.transportValueData = {};
rehydrationContext.incomingResults = [];
return (
<script
dangerouslySetInnerHTML={{
__html,
}}
/>
);
},
};
return rehydrationContext;
}