2023-02-16 22:39:57 +00:00
|
|
|
import type { GetServerSidePropsContext } from "next";
|
2023-05-09 19:27:05 +00:00
|
|
|
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
|
2021-10-20 16:00:11 +00:00
|
|
|
import superjson from "superjson";
|
|
|
|
|
2023-05-09 19:27:05 +00:00
|
|
|
import { getLocaleFromHeaders } from "@calcom/lib/i18n";
|
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-05-09 19:27:05 +00:00
|
|
|
const ctx = await createContext(context);
|
|
|
|
const locale = getLocaleFromHeaders(context.req);
|
|
|
|
const i18n = await serverSideTranslations(getLocaleFromHeaders(context.req), ["common", "vital"]);
|
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,
|
2023-05-09 19:27:05 +00:00
|
|
|
ctx: { ...ctx, locale, i18n },
|
2021-10-20 16:00:11 +00:00
|
|
|
});
|
|
|
|
|
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();
|
2023-06-07 02:03:52 +00:00
|
|
|
// So feature flags are available on first render
|
|
|
|
await ssr.viewer.features.map.prefetch();
|
2021-10-20 16:00:11 +00:00
|
|
|
|
|
|
|
return ssr;
|
|
|
|
}
|