Skip to content

Commit

Permalink
Implement ssrInit
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjORbj committed Nov 2, 2023
1 parent aa311cf commit c981f2e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions apps/web/app/_trpc/ssrInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { SSRConfig } from "next-i18next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { headers } from "next/headers";
import superjson from "superjson";

import { CALCOM_VERSION } from "@calcom/lib/constants";
import prisma from "@calcom/prisma";
import { appRouter } from "@calcom/trpc/server/routers/_app";

import { createTRPCNextLayout } from "./createTRPCNextLayout";

export async function ssrInit(options?: { noI18nPreload: boolean }) {
const locale = headers().get("x-locale") ?? "en";

const i18n = (await serverSideTranslations(locale, ["common", "vital"])) || "en";

const ssr = createTRPCNextLayout({
router: appRouter,
transformer: superjson,
createContext() {
return { prisma, session: null, locale, i18n: i18n as unknown as SSRConfig };
},
});

// i18n translations are already retrieved from serverSideTranslations call, there is no need to run a i18n.fetch
// we can set query data directly to the queryClient
const queryKey = [
["viewer", "public", "i18n"],
{ input: { locale, CalComVersion: CALCOM_VERSION }, type: "query" },
];
if (!options?.noI18nPreload) {
ssr.queryClient.setQueryData(queryKey, { i18n });
}

await Promise.allSettled([
// 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(),
]);

return ssr;
}

0 comments on commit c981f2e

Please sign in to comment.