-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry-hattip.tsx
55 lines (47 loc) · 1.4 KB
/
entry-hattip.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
import { createRequestHandler } from "rakkasjs";
import {
QueryCache,
QueryClient,
QueryClientProvider,
} from "@tanstack/react-query";
import { uneval } from "devalue";
export default createRequestHandler({
createPageHooks() {
let queries = Object.create(null);
return {
wrapApp(app) {
const queryCache = new QueryCache({
onSuccess(data, query) {
// Store newly fetched data
queries[query.queryHash] = data;
},
});
const queryClient = new QueryClient({
queryCache,
defaultOptions: {
queries: {
suspense: true,
staleTime: Infinity,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
},
},
});
return (
<QueryClientProvider client={queryClient}>{app}</QueryClientProvider>
);
},
emitToDocumentHead() {
return `<script>$TQD=Object.create(null);$TQS=data=>Object.assign($TQD,data);</script>`;
},
emitBeforeSsrChunk() {
if (Object.keys(queries).length === 0) return "";
// Emit a script that calls the global $TQS function with the
// newly fetched query data.
const queriesString = uneval(queries);
queries = Object.create(null);
return `<script>$TQS(${queriesString})</script>`;
},
};
},
});