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-10-10 16:36:28 +00:00
|
|
|
import { getLocale } from "@calcom/features/auth/lib/getLocale";
|
2023-08-22 12:34:55 +00:00
|
|
|
import { CALCOM_VERSION } from "@calcom/lib/constants";
|
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.
|
|
|
|
*/
|
2023-09-20 20:25:38 +00:00
|
|
|
export async function ssrInit(context: GetServerSidePropsContext, options?: { noI18nPreload: boolean }) {
|
2023-05-09 19:27:05 +00:00
|
|
|
const ctx = await createContext(context);
|
2023-10-10 16:36:28 +00:00
|
|
|
const locale = await getLocale(context.req);
|
2023-08-22 12:34:55 +00:00
|
|
|
const i18n = await serverSideTranslations(locale, ["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
|
|
|
});
|
|
|
|
|
2023-08-22 12:34:55 +00:00
|
|
|
await Promise.allSettled([
|
|
|
|
// always preload "viewer.public.i18n"
|
2023-09-20 20:25:38 +00:00
|
|
|
!options?.noI18nPreload
|
|
|
|
? ssr.viewer.public.i18n.prefetch({ locale, CalComVersion: CALCOM_VERSION })
|
|
|
|
: Promise.resolve({}),
|
2023-08-22 12:34:55 +00:00
|
|
|
// So feature flags are available on first render
|
|
|
|
ssr.viewer.features.map.prefetch(),
|
|
|
|
// Provides a better UX to the users who have already upgraded.
|
|
|
|
ssr.viewer.teams.hasTeamPlan.prefetch(),
|
|
|
|
ssr.viewer.public.session.prefetch(),
|
|
|
|
]);
|
2023-08-09 08:55:27 +00:00
|
|
|
|
2021-10-20 16:00:11 +00:00
|
|
|
return ssr;
|
|
|
|
}
|