2023-02-16 22:39:57 +00:00
|
|
|
import type { GetStaticPropsContext } from "next";
|
2021-12-14 12:31:54 +00:00
|
|
|
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
|
|
|
|
import superjson from "superjson";
|
|
|
|
|
2022-07-22 17:27:06 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-11-10 23:40:01 +00:00
|
|
|
import { createProxySSGHelpers } from "@calcom/trpc/react/ssg";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { appRouter } from "@calcom/trpc/server/routers/_app";
|
2021-12-14 12:31:54 +00:00
|
|
|
|
2023-06-22 22:25:37 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
const { i18n } = require("@calcom/config/next-i18next.config");
|
|
|
|
|
2023-08-19 00:46:17 +00:00
|
|
|
// TODO: Consolidate this constant
|
|
|
|
// eslint-disable-next-line turbo/no-undeclared-env-vars
|
|
|
|
const CalComVersion = process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA || "NA";
|
|
|
|
|
2021-12-14 12:31:54 +00:00
|
|
|
/**
|
|
|
|
* Initialize static site rendering tRPC helpers.
|
|
|
|
* Provides a method to prefetch tRPC-queries in a `getStaticProps`-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.
|
|
|
|
*/
|
|
|
|
export async function ssgInit<TParams extends { locale?: string }>(opts: GetStaticPropsContext<TParams>) {
|
|
|
|
const requestedLocale = opts.params?.locale || opts.locale || i18n.defaultLocale;
|
|
|
|
const isSupportedLocale = i18n.locales.includes(requestedLocale);
|
|
|
|
if (!isSupportedLocale) {
|
|
|
|
console.warn(`Requested unsupported locale "${requestedLocale}"`);
|
|
|
|
}
|
|
|
|
const locale = isSupportedLocale ? requestedLocale : i18n.defaultLocale;
|
|
|
|
|
|
|
|
const _i18n = await serverSideTranslations(locale, ["common"]);
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const ssg = createProxySSGHelpers({
|
2021-12-14 12:31:54 +00:00
|
|
|
router: appRouter,
|
|
|
|
transformer: superjson,
|
|
|
|
ctx: {
|
|
|
|
prisma,
|
|
|
|
session: null,
|
|
|
|
locale,
|
|
|
|
i18n: _i18n,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// always preload i18n
|
2023-08-19 00:46:17 +00:00
|
|
|
await ssg.viewer.public.i18n.fetch({ locale, CalComVersion });
|
2021-12-14 12:31:54 +00:00
|
|
|
|
|
|
|
return ssg;
|
|
|
|
}
|