2023-02-16 22:39:57 +00:00
|
|
|
import type { GetServerSidePropsContext } from "next";
|
2021-10-20 16:00:11 +00:00
|
|
|
import superjson from "superjson";
|
|
|
|
|
2023-03-10 23:45:24 +00:00
|
|
|
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
2022-11-10 23:40:01 +00:00
|
|
|
import { createProxySSGHelpers } from "@calcom/trpc/react/ssg";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { createContext } from "@calcom/trpc/server/createContext";
|
|
|
|
import { appRouter } from "@calcom/trpc/server/routers/_app";
|
2021-10-20 16:00:11 +00:00
|
|
|
|
2021-12-14 12:31:54 +00:00
|
|
|
/**
|
|
|
|
* Initialize server-side rendering tRPC helpers.
|
|
|
|
* Provides a method to prefetch tRPC-queries in a `getServerSideProps`-function.
|
|
|
|
* Automatically prefetches i18n based on the passed in `context`-object to prevent i18n-flickering.
|
|
|
|
* Make sure to `return { props: { trpcState: ssr.dehydrate() } }` at the end.
|
|
|
|
*/
|
2021-10-20 16:00:11 +00:00
|
|
|
export async function ssrInit(context: GetServerSidePropsContext) {
|
2023-03-10 23:45:24 +00:00
|
|
|
const { req, res } = context;
|
|
|
|
|
|
|
|
const sessionGetter = () => getServerSession({ req, res });
|
|
|
|
|
|
|
|
const ctx = await createContext(context, sessionGetter);
|
2021-10-20 16:00:11 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const ssr = createProxySSGHelpers({
|
2021-10-20 16:00:11 +00:00
|
|
|
router: appRouter,
|
|
|
|
transformer: superjson,
|
|
|
|
ctx,
|
|
|
|
});
|
|
|
|
|
2022-06-19 15:02:00 +00:00
|
|
|
// always preload "viewer.public.i18n"
|
2022-11-10 23:40:01 +00:00
|
|
|
await ssr.viewer.public.i18n.fetch();
|
2021-10-20 16:00:11 +00:00
|
|
|
|
|
|
|
return ssr;
|
|
|
|
}
|