-
Notifications
You must be signed in to change notification settings - Fork 2
/
entry-client.tsx
71 lines (65 loc) · 1.99 KB
/
entry-client.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
/* eslint-disable no-var */
import { startClient } from "rakkasjs";
import { QueryClientProvider, QueryClient, MutationCache } from "@tanstack/react-query";
import { TypedPocketBase } from "typed-pocketbase";
import PocketBase from "pocketbase";
import { Schema } from "./lib/pb/db-types";
const queryClient:QueryClient = new QueryClient({
mutationCache: new MutationCache({
onSuccess: async (data, variable, context, mutation) => {
if (Array.isArray(mutation.meta?.invalidates)) {
return queryClient.invalidateQueries({
queryKey: mutation.meta?.invalidates,
});
}
},
}),
defaultOptions: {
queries: {
staleTime: 100,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
},
},
});
function setQueryData(data: Record<string, unknown>) {
for (const [key, value] of Object.entries(data)) {
queryClient.setQueryData(JSON.parse(key), value, { updatedAt: Date.now() });
}
}
declare global {
var $TQD: Record<string, unknown> | undefined;
var $TQS: typeof setQueryData;
}
// Insert data that was already streamed before this point
setQueryData(globalThis.$TQD ?? {});
// Delete the global variable so that it doesn't get serialized again
delete globalThis.$TQD;
// From now on, insert data directly
globalThis.$TQS = setQueryData;
startClient({
hooks: {
wrapApp(app) {
return (
<QueryClientProvider client={queryClient}>{app}</QueryClientProvider>
);
},
beforeStart() {
// Do something before starting the client
},
extendPageContext(ctx) {
// console.log("============= CLIENT SIDE PB ==============",ctx.locals.pb)
if (!ctx.locals.pb) {
ctx.locals.pb = new PocketBase(
import.meta.env.RAKKAS_PB_URL,
) as TypedPocketBase<Schema>;
ctx.locals.pb?.authStore.onChange(() => {
ctx.requestContext?.setCookie?.(
"set-cookie",
ctx.locals.pb?.authStore.exportToCookie(),
);
});
}
},
},
});