-
Notifications
You must be signed in to change notification settings - Fork 2
/
entry-hattip.tsx
137 lines (124 loc) · 4.14 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { RequestContext, createRequestHandler } from "rakkasjs";
import {
MutationCache,
QueryCache,
QueryClient,
QueryClientProvider,
} from "@tanstack/react-query";
import { uneval } from "devalue";
import { cookie } from "@hattip/cookie";
import PocketBase from "pocketbase";
import { TypedPocketBase } from "typed-pocketbase";
import { Schema } from "./lib/pb/db-types";
export async function beforePageLuciaMiddleware(ctx: RequestContext<unknown>) {}
export default createRequestHandler({
middleware: {
beforePages: [
cookie(),
(ctx) => {
ctx.locals.pb = new PocketBase(
import.meta.env.RAKKAS_PB_URL,
) as TypedPocketBase<Schema>;
// load the store data from the request cookie string
ctx.locals.pb.authStore.loadFromCookie(
ctx.request.headers.get("cookie") || "",
);
},
],
beforeApiRoutes: [],
beforeNotFound: [],
},
createPageHooks(requestContext) {
let queries = Object.create(null);
return {
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>`;
},
emitToDocumentHead() {
const cookie_theme = requestContext?.cookie?.theme;
return `
<link rel="icon" type="image/svg+xml" href="/site.svg" />
<script>
(function() {
document.documentElement.setAttribute("data-theme", "${cookie_theme}");
})();
</script>
<script>$TQD=Object.create(null);$TQS=data=>Object.assign($TQD,data);</script>
`;
},
async extendPageContext(ctx) {
const request = ctx.requestContext?.request;
if (!request) return;
if (!ctx.locals.pb) {
ctx.locals.pb = new PocketBase(
import.meta.env.RAKKAS_PB_URL,
) as TypedPocketBase<Schema>;
// load the store data from the request cookie string
ctx.locals.pb.authStore.loadFromCookie(
request.headers.get("cookie") || "",
);
}
try {
if (ctx.locals.pb.authStore.isValid) {
const user = ctx?.locals?.pb;
ctx.queryClient.setQueryData("user", user?.authStore?.model);
// console.log("===VALID USER , UPDATING POCKETBASE USER= ===");
} else {
// console.log("====INVALID USER , LOGGING OUT POCKETBASE= ===");
ctx.locals.pb.authStore.clear();
ctx.queryClient.setQueryData("user", null);
}
} catch (_) {
// clear the auth store on failed refresh
ctx.locals.pb.authStore.clear();
}
},
wrapApp(app) {
const queryCache = new QueryCache({
onSuccess(data, query) {
queries[query.queryHash] = data;
},
});
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,
});
}
},
}),
queryCache,
defaultOptions: {
queries: {
staleTime: Infinity,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
},
},
});
return (
<QueryClientProvider client={queryClient}>{app}</QueryClientProvider>
);
},
// wrapSsrStream(stream) {
// const { readable, writable } = new TransformStream({
// transform(chunk, controller) {
// // You can transform the chunks of the
// // React SSR stream here.
// controller.enqueue(chunk);
// },
// });
// // @ts-expect-error
// stream.pipeThrough(writable);
// return readable;
// },
};
},
});