chore: [app-router-migration 39] refactor ssr/ssg functions (#13336)

This commit is contained in:
DmytroHryshyn authored and GitHub committed 2024-01-19 15:58:37 -07:00
1 parent 5685d343b1
commit ff1aefe539
21 files changed
+168 -81

No files matched your search

+3 -3
View File
@@ -2,13 +2,13 @@ import NotFoundPage from "@pages/404";
import { WithLayout } from "app/layoutHOC";
import type { GetStaticPropsContext } from "next";
import { ssgInit } from "@server/lib/ssg";
import { getTranslations } from "@server/lib/getTranslations";
const getData = async (context: GetStaticPropsContext) => {
const ssg = await ssgInit(context);
const i18n = await getTranslations(context);
return {
dehydratedState: ssg.dehydrate(),
i18n,
};
};
+2 -1
View File
@@ -37,6 +37,7 @@ export type AppProps = Omit<
WithNonceProps<{
themeBasis?: string;
session: Session;
i18n?: SSRConfig;
}>
>
>,
@@ -108,7 +109,7 @@ const CustomI18nextProvider = (props: AppPropsWithoutNonce) => {
}, [locale]);
const clientViewerI18n = useViewerI18n(locale);
const i18n = clientViewerI18n.data?.i18n;
const i18n = clientViewerI18n.data?.i18n ?? props.pageProps.i18n;
const passedProps = {
...props,
@@ -1,7 +1,7 @@
import { type GetStaticProps } from "next";
import { z } from "zod";
import { ssgInit } from "@server/lib/ssg";
import { getTranslations } from "@server/lib/getTranslations";
const validStatuses = ["upcoming", "recurring", "past", "cancelled", "unconfirmed"] as const;
@@ -11,14 +11,14 @@ const querySchema = z.object({
export const getStaticProps: GetStaticProps = async (ctx) => {
const params = querySchema.safeParse(ctx.params);
const ssg = await ssgInit(ctx);
const i18n = await getTranslations(ctx);
if (!params.success) return { notFound: true };
return {
props: {
status: params.data.status,
trpcState: ssg.dehydrate(),
i18n,
},
};
};
+3 -3
View File
@@ -17,7 +17,7 @@ import { Discord } from "@calcom/ui/components/icon/Discord";
import PageWrapper from "@components/PageWrapper";
import { ssgInit } from "@server/lib/ssg";
import { getTranslations } from "@server/lib/getTranslations";
enum pageType {
ORG = "org",
@@ -273,11 +273,11 @@ export default function Custom404() {
Custom404.PageWrapper = PageWrapper;
export const getStaticProps = async (context: GetStaticPropsContext) => {
const ssr = await ssgInit(context);
const i18n = await getTranslations(context);
return {
props: {
trpcState: ssr.dehydrate(),
i18n,
},
};
};
+3 -3
View File
@@ -10,7 +10,7 @@ import { X } from "@calcom/ui/components/icon";
import PageWrapper from "@components/PageWrapper";
import AuthContainer from "@components/ui/AuthContainer";
import { ssgInit } from "@server/lib/ssg";
import { getTranslations } from "@server/lib/getTranslations";
const querySchema = z.object({
error: z.string().optional(),
@@ -50,11 +50,11 @@ export default function Error() {
Error.PageWrapper = PageWrapper;
export const getStaticProps = async (context: GetStaticPropsContext) => {
const ssr = await ssgInit(context);
const i18n = await getTranslations(context);
return {
props: {
trpcState: ssr.dehydrate(),
i18n,
},
};
};
+22
View File
@@ -0,0 +1,22 @@
import type { GetStaticPropsContext } from "next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { i18n } = require("@calcom/config/next-i18next.config");
export async function getTranslations<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"]);
return {
i18n: _i18n,
};
}
+42 -2
View File
@@ -2,11 +2,51 @@ import type { GetServerSidePropsContext } from "next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import superjson from "superjson";
import { forms } from "@calcom/app-store/routing-forms/trpc/procedures/forms";
import { getLocale } from "@calcom/features/auth/lib/getLocale";
import { map } from "@calcom/features/flags/server/procedures/map";
import { CALCOM_VERSION } from "@calcom/lib/constants";
import { createProxySSGHelpers } from "@calcom/trpc/react/ssg";
import { createContext } from "@calcom/trpc/server/createContext";
import { appRouter } from "@calcom/trpc/server/routers/_app";
import { me } from "@calcom/trpc/server/routers/loggedInViewer/procedures/me";
import { teamsAndUserProfilesQuery } from "@calcom/trpc/server/routers/loggedInViewer/procedures/teamsAndUserProfilesQuery";
import { event } from "@calcom/trpc/server/routers/publicViewer/procedures/event";
import { session } from "@calcom/trpc/server/routers/publicViewer/procedures/session";
import { get } from "@calcom/trpc/server/routers/viewer/eventTypes/procedures/get";
import { hasTeamPlan } from "@calcom/trpc/server/routers/viewer/teams/procedures/hasTeamPlan";
import { router, mergeRouters } from "@calcom/trpc/server/trpc";
const loggedInRouter = router({
me,
});
// Temporary workaround for OOM issue, import only procedures that are called on the server side
const routerSlice = router({
viewer: mergeRouters(
loggedInRouter,
router({
features: router({
map,
}),
public: router({
session,
event,
}),
teams: router({
hasTeamPlan,
}),
appRoutingForms: router({
forms,
}),
teamsAndUserProfilesQuery: router({
teamsAndUserProfilesQuery,
}),
eventTypes: router({
get,
}),
})
),
});
/**
* Initialize server-side rendering tRPC helpers.
@@ -20,7 +60,7 @@ export async function ssrInit(context: GetServerSidePropsContext, options?: { no
const i18n = await serverSideTranslations(locale, ["common", "vital"]);
const ssr = createProxySSGHelpers({
router: appRouter,
router: routerSlice,
transformer: superjson,
ctx: { ...ctx, locale, i18n },
});