perf: Remove ssrInit completely (#20439)

* remove ssrInit completely

* remove instances of dehydratedState

* fix

* refactor

* fix type checks

* fix linting

* Refactor

* Refactor

* remove log
This commit is contained in:
Benny Joo
2025-04-01 19:14:46 -04:00
committed by GitHub
parent b5b4134b75
commit 75c79f76c8
30 changed files with 70 additions and 209 deletions
@@ -29,7 +29,7 @@ const getData = withAppDirSsr<ClientPageProps>(getServerSideProps);
const ServerPage = async ({ params, searchParams }: ServerPageProps) => {
const context = buildLegacyCtx(await headers(), await cookies(), await params, await searchParams);
const { dehydratedState, ...props } = await getData(context);
const props = await getData(context);
return <SetupView {...props} />;
};
-4
View File
@@ -17,9 +17,5 @@ export const withAppDirSsr =
return {
...props,
// includes dehydratedState required for future page trpcPropvider
...("trpcState" in props && {
dehydratedState: props.trpcState,
}),
};
};
-3
View File
@@ -42,9 +42,6 @@ const withEmbedSsrAppDir =
return {
...ssrResponse.props,
...("trpcState" in ssrResponse.props && {
dehydratedState: ssrResponse.props.trpcState,
}),
isEmbed: true,
};
};
+11
View File
@@ -0,0 +1,11 @@
import { cookies, headers } from "next/headers";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import { createContext } from "@calcom/trpc/server/createContext";
import { buildLegacyRequest } from "@lib/buildLegacyCtx";
export const getTRPCContext = async () => {
const legacyReq = buildLegacyRequest(await headers(), await cookies());
return await createContext({ req: legacyReq, res: {} as any }, getServerSession);
};
@@ -25,7 +25,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
return {
props: {
categories: Object.entries(categories).map(([name, count]) => ({ name, count })),
trpcState: undefined,
},
};
};
-1
View File
@@ -42,7 +42,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
}),
appStore,
userAdminTeams: userAdminTeamsIds,
trpcState: undefined,
},
};
};
@@ -7,8 +7,6 @@ import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import prisma from "@calcom/prisma";
import type { AppGetServerSidePropsContext } from "@calcom/types/AppGetServerSideProps";
import { ssrInit } from "@server/lib/ssr";
const paramsSchema = z.object({
pages: z.array(z.string()),
});
@@ -43,7 +41,7 @@ export async function getServerSideProps(
};
}
const result = await getServerSideProps(context as AppGetServerSidePropsContext, prisma, user, ssrInit);
const result = await getServerSideProps(context as AppGetServerSidePropsContext, prisma, user);
if (result.notFound) {
return { notFound: true };
@@ -1,4 +1,5 @@
import type { EmbedProps } from "app/WithEmbedSSR";
import { getTRPCContext } from "app/_trpc/context";
import type { GetServerSidePropsContext } from "next";
import { z } from "zod";
@@ -10,6 +11,8 @@ import { UserRepository } from "@calcom/lib/server/repository/user";
import slugify from "@calcom/lib/slugify";
import prisma from "@calcom/prisma";
import { RedirectType } from "@calcom/prisma/enums";
import { publicViewerRouter } from "@calcom/trpc/server/routers/publicViewer/_router";
import { createCallerFactory } from "@calcom/trpc/server/trpc";
import { getTemporaryOrgRedirect } from "@lib/getTemporaryOrgRedirect";
import type { inferSSRProps } from "@lib/types/inferSSRProps";
@@ -23,9 +26,6 @@ async function getUserPageProps(context: GetServerSidePropsContext) {
const { currentOrgDomain, isValidOrgDomain } = orgDomainConfig(context.req);
const org = isValidOrgDomain ? currentOrgDomain : null;
const { ssrInit } = await import("@server/lib/ssr");
const ssr = await ssrInit(context);
const hashedLink = await prisma.hashedLink.findUnique({
where: {
link,
@@ -115,7 +115,10 @@ async function getUserPageProps(context: GetServerSidePropsContext) {
// We use this to both prefetch the query on the server,
// as well as to check if the event exist, so we c an show a 404 otherwise.
const eventData = await ssr.viewer.public.event.fetch({
const trpcContext = await getTRPCContext();
const createCaller = createCallerFactory(publicViewerRouter);
const caller = createCaller(trpcContext);
const eventData = await caller.event({
username: name,
eventSlug: slug,
isTeamEvent,
@@ -140,7 +143,6 @@ async function getUserPageProps(context: GetServerSidePropsContext) {
booking,
user: name,
slug,
trpcState: ssr.dehydrate(),
isBrandingHidden: hideBranding,
// Sending the team event from the server, because this template file
// is reused for both team and user events.
@@ -1,14 +1,15 @@
import { getTRPCContext } from "app/_trpc/context";
import type { GetServerSidePropsContext } from "next";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import { eventTypesRouter } from "@calcom/trpc/server/routers/viewer/eventTypes/_router";
import { createCallerFactory } from "@calcom/trpc/server/trpc";
import { asStringOrThrow } from "@lib/asStringOrNull";
import type { inferSSRProps } from "@lib/types/inferSSRProps";
import { ssrInit } from "@server/lib/ssr";
export type PageProps = inferSSRProps<typeof getServerSideProps>;
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
@@ -17,7 +18,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
const session = await getServerSession({ req });
const typeParam = parseInt(asStringOrThrow(query.type));
const ssr = await ssrInit(context);
if (Number.isNaN(typeParam)) {
const notFound = {
@@ -37,9 +37,11 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
return redirect;
}
const getEventTypeById = async (eventTypeId: number) => {
await ssr.viewer.eventTypes.get.prefetch({ id: eventTypeId });
const trpcContext = await getTRPCContext();
const createCaller = createCallerFactory(eventTypesRouter);
const caller = createCaller(trpcContext);
try {
const { eventType } = await ssr.viewer.eventTypes.get.fetch({ id: eventTypeId });
const { eventType } = await caller.get({ id: eventTypeId });
return eventType;
} catch (e: unknown) {
logger.error(safeStringify(e));
@@ -61,7 +63,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
props: {
eventType,
type: typeParam,
trpcState: ssr.dehydrate(),
},
};
};
@@ -1,11 +1,8 @@
import type { GetServerSidePropsContext } from "next";
import { getLocale } from "@calcom/features/auth/lib/getLocale";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import { UserRepository } from "@calcom/lib/server/repository/user";
import { ssrInit } from "@server/lib/ssr";
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
const { req } = context;
@@ -15,10 +12,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
return { redirect: { permanent: false, destination: "/auth/login" } };
}
const ssr = await ssrInit(context);
await ssr.viewer.me.get.prefetch();
const user = await UserRepository.findUserTeams({
id: session.user.id,
});
@@ -27,10 +20,8 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
throw new Error("User from session not found");
}
const locale = await getLocale(context.req);
return {
props: {
trpcState: ssr.dehydrate(),
hasPendingInvites: user.teams.find((team) => team.accepted === false) ?? false,
},
};
@@ -1,3 +1,4 @@
import { getTRPCContext } from "app/_trpc/context";
import type { GetServerSidePropsContext } from "next";
import { z } from "zod";
@@ -6,6 +7,8 @@ import { getSlugOrRequestedSlug } from "@calcom/features/ee/organizations/lib/or
import { orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains";
import slugify from "@calcom/lib/slugify";
import prisma from "@calcom/prisma";
import { publicViewerRouter } from "@calcom/trpc/server/routers/publicViewer/_router";
import { createCallerFactory } from "@calcom/trpc/server/trpc";
const paramsSchema = z.object({
type: z.string().transform((s) => slugify(s)),
@@ -36,10 +39,10 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
}
const org = isValidOrgDomain ? currentOrgDomain : null;
const { ssrInit } = await import("@server/lib/ssr");
const ssr = await ssrInit(context);
const eventData = await ssr.viewer.public.event.fetch({
const trpcContext = await getTRPCContext();
const createCaller = createCallerFactory(publicViewerRouter);
const caller = createCaller(trpcContext);
const eventData = await caller.event({
username: teamSlug,
eventSlug: meetingSlug,
isTeamEvent: true,
@@ -67,7 +70,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
user: teamSlug,
teamId: team.id,
slug: meetingSlug,
trpcState: ssr.dehydrate(),
isBrandingHidden: team?.hideBranding,
themeBasis: null,
},
+1 -2
View File
@@ -11,7 +11,6 @@ import slugify from "@calcom/lib/slugify";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { IS_GOOGLE_LOGIN_ENABLED } from "@server/lib/constants";
import { ssrInit } from "@server/lib/ssr";
const checkValidEmail = (email: string) => emailSchema.safeParse(email).success;
@@ -26,7 +25,7 @@ const querySchema = z.object({
export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
const prisma = await import("@calcom/prisma").then((mod) => mod.default);
const emailVerificationEnabled = await getFeatureFlag(prisma, "email-verification");
await ssrInit(ctx);
const signupDisabled = await getFeatureFlag(prisma, "disable-signup");
const token = z.string().optional().parse(ctx.query.token);
@@ -14,8 +14,6 @@ import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import { getTemporaryOrgRedirect } from "@lib/getTemporaryOrgRedirect";
import { ssrInit } from "@server/lib/ssr";
const paramsSchema = z.object({
type: z.string().transform((s) => slugify(s)),
slug: z.string().transform((s) => slugify(s)),
@@ -59,7 +57,7 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
const name = team.parent?.name ?? team.name ?? null;
const booking = rescheduleUid ? await getBookingForReschedule(`${rescheduleUid}`, session?.user?.id) : null;
const ssr = await ssrInit(context);
const fromRedirectOfNonOrgLink = context.query.orgRedirection === "true";
const isUnpublished = team.parent ? !team.parent.slug : !team.slug;
@@ -125,7 +123,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
user: teamSlug,
teamId: team.id,
slug: meetingSlug,
trpcState: ssr.dehydrate(),
isBrandingHidden: team?.hideBranding,
isInstantMeeting: eventData && queryIsInstantMeeting ? true : false,
themeBasis: null,
@@ -21,8 +21,6 @@ import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { getTemporaryOrgRedirect } from "@lib/getTemporaryOrgRedirect";
import { ssrInit } from "@server/lib/ssr";
const log = logger.getSubLogger({ prefix: ["team/[slug]"] });
function getOrgProfileRedirectToVerifiedDomain(
@@ -103,7 +101,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
}
}
const ssr = await ssrInit(context);
const metadata = teamMetadataSchema.parse(team?.metadata ?? {});
// Taking care of sub-teams and orgs
@@ -149,7 +146,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
parent: teamParent,
createdAt: null,
},
trpcState: ssr.dehydrate(),
},
} as const;
}
@@ -215,7 +211,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
props: {
considerUnpublished: true,
team: { ...serializableTeam },
trpcState: ssr.dehydrate(),
},
} as const;
}
@@ -230,7 +225,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
children: isTeamOrParentOrgPrivate ? [] : team.children,
},
themeBasis: serializableTeam.slug,
trpcState: ssr.dehydrate(),
markdownStrippedBio,
isValidOrgDomain,
currentOrgDomain,
@@ -15,15 +15,11 @@ import { OrganizationRepository } from "@calcom/lib/server/repository/organizati
import { UserRepository } from "@calcom/lib/server/repository/user";
import prisma from "@calcom/prisma";
import { ssrInit } from "@server/lib/ssr";
const md = new MarkdownIt("default", { html: true, breaks: true, linkify: true });
export async function getServerSideProps(context: GetServerSidePropsContext) {
const { req } = context;
const ssr = await ssrInit(context);
const booking = await BookingRepository.findBookingForMeetingPage({
bookingUid: context.query.uid as string,
});
@@ -153,7 +149,6 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
},
hasTeamPlan: !!hasTeamPlan,
calVideoLogo,
trpcState: ssr.dehydrate(),
},
};
}
@@ -1,3 +1,4 @@
import { getTRPCContext } from "app/_trpc/context";
import type { GetServerSidePropsContext } from "next";
import { z } from "zod";
@@ -11,11 +12,11 @@ import { maybeGetBookingUidFromSeat } from "@calcom/lib/server/maybeGetBookingUi
import { BookingRepository } from "@calcom/lib/server/repository/booking";
import prisma from "@calcom/prisma";
import { customInputSchema, EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import { meRouter } from "@calcom/trpc/server/routers/viewer/me/_router";
import { createCallerFactory } from "@calcom/trpc/server/trpc";
import type { inferSSRProps } from "@lib/types/inferSSRProps";
import { ssrInit } from "@server/lib/ssr";
const stringToBoolean = z
.string()
.optional()
@@ -45,13 +46,15 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
"@lib/booking"
);
const ssr = await ssrInit(context);
const session = await getServerSession({ req: context.req });
let tz: string | null = null;
let userTimeFormat: number | null = null;
let requiresLoginToUpdate = false;
if (session) {
const user = await ssr.viewer.me.get.fetch();
const trpcContext = await getTRPCContext();
const createCaller = createCallerFactory(meRouter);
const caller = createCaller(trpcContext);
const user = await caller.get();
tz = user.timeZone;
userTimeFormat = user.timeFormat;
}
@@ -218,7 +221,6 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
profile,
eventType,
recurringBookings: await getRecurringBookings(bookingInfo.recurringEventId),
trpcState: ssr.dehydrate(),
dynamicEventName: bookingInfo?.eventType?.eventName || "",
bookingInfo,
paymentStatus: payment,
@@ -12,10 +12,6 @@ vi.mock("@calcom/lib/constants", async () => {
function mockedUserPageComponentProps(props: Partial<React.ComponentProps<typeof UserPage>>) {
return {
trpcState: {
mutations: [],
queries: [],
},
themeBasis: "dark",
safeBio: "My Bio",
profile: {
@@ -1,4 +1,4 @@
import type { DehydratedState } from "@tanstack/react-query";
import { getTRPCContext } from "app/_trpc/context";
import { type GetServerSidePropsContext } from "next";
import type { Session } from "next-auth";
import { z } from "zod";
@@ -13,6 +13,8 @@ import { UserRepository } from "@calcom/lib/server/repository/user";
import slugify from "@calcom/lib/slugify";
import prisma from "@calcom/prisma";
import { RedirectType } from "@calcom/prisma/client";
import { publicViewerRouter } from "@calcom/trpc/server/routers/publicViewer/_router";
import { createCallerFactory } from "@calcom/trpc/server/trpc";
import { getTemporaryOrgRedirect } from "@lib/getTemporaryOrgRedirect";
@@ -40,7 +42,6 @@ type Props = {
bookingUid: string | null;
user: string;
slug: string;
trpcState: DehydratedState;
isBrandingHidden: boolean;
isSEOIndexable: boolean | null;
themeBasis: null | string;
@@ -103,8 +104,6 @@ async function getDynamicGroupPageProps(context: GetServerSidePropsContext) {
const { user: usernames, type: slug } = paramsSchema.parse(context.params);
const { rescheduleUid, bookingUid } = context.query;
const { ssrInit } = await import("@server/lib/ssr");
const ssr = await ssrInit(context);
const { currentOrgDomain, isValidOrgDomain } = orgDomainConfig(context.req, context.params?.orgSlug);
const org = isValidOrgDomain ? currentOrgDomain : null;
if (!org) {
@@ -135,7 +134,10 @@ async function getDynamicGroupPageProps(context: GetServerSidePropsContext) {
// We use this to both prefetch the query on the server,
// as well as to check if the event exist, so we c an show a 404 otherwise.
const eventData = await ssr.viewer.public.event.fetch({
const trpcContext = await getTRPCContext();
const createCaller = createCallerFactory(publicViewerRouter);
const caller = createCaller(trpcContext);
const eventData = await caller.event({
username: usernames.join("+"),
eventSlug: slug,
org,
@@ -171,7 +173,6 @@ async function getDynamicGroupPageProps(context: GetServerSidePropsContext) {
},
user: usernames.join("+"),
slug,
trpcState: ssr.dehydrate(),
isBrandingHidden: false,
isSEOIndexable: true,
themeBasis: null,
@@ -215,8 +216,6 @@ async function getUserPageProps(context: GetServerSidePropsContext) {
}
}
const { ssrInit } = await import("@server/lib/ssr");
const ssr = await ssrInit(context);
const [user] = await UserRepository.findUsersByUsername({
usernameList: [username],
orgSlug: isValidOrgDomain ? currentOrgDomain : null,
@@ -231,7 +230,10 @@ async function getUserPageProps(context: GetServerSidePropsContext) {
const org = isValidOrgDomain ? currentOrgDomain : null;
// We use this to both prefetch the query on the server,
// as well as to check if the event exist, so we can show a 404 otherwise.
const eventData = await ssr.viewer.public.event.fetch({
const trpcContext = await getTRPCContext();
const createCaller = createCallerFactory(publicViewerRouter);
const caller = createCaller(trpcContext);
const eventData = await caller.event({
username,
eventSlug: slug,
org,
@@ -270,7 +272,6 @@ async function getUserPageProps(context: GetServerSidePropsContext) {
},
user: username,
slug,
trpcState: ssr.dehydrate(),
isBrandingHidden: user?.hideBranding,
isSEOIndexable: allowSEOIndexing,
themeBasis: username,
@@ -1,4 +1,3 @@
import type { DehydratedState } from "@tanstack/react-query";
import type { EmbedProps } from "app/WithEmbedSSR";
import type { GetServerSideProps } from "next";
import { encode } from "querystring";
@@ -20,11 +19,8 @@ import type { UserProfile } from "@calcom/types/UserProfile";
import { getTemporaryOrgRedirect } from "@lib/getTemporaryOrgRedirect";
import { ssrInit } from "@server/lib/ssr";
const log = logger.getSubLogger({ prefix: ["[[pages/[user]]]"] });
type UserPageProps = {
trpcState: DehydratedState;
profile: {
name: string;
image: string;
@@ -74,7 +70,6 @@ type UserPageProps = {
} & EmbedProps;
export const getServerSideProps: GetServerSideProps<UserPageProps> = async (context) => {
const ssr = await ssrInit(context);
const { currentOrgDomain, isValidOrgDomain } = orgDomainConfig(context.req, context.params?.orgSlug);
const usernameList = getUsernameList(context.query.user as string);
@@ -197,7 +192,6 @@ export const getServerSideProps: GetServerSideProps<UserPageProps> = async (cont
profile,
// Dynamic group has no theme preference right now. It uses system theme.
themeBasis: user.username,
trpcState: ssr.dehydrate(),
markdownStrippedBio,
isOrgSEOIndexable: org?.organizationSettings?.allowSEOIndexing ?? false,
},
@@ -9,13 +9,11 @@ import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
import prisma from "@calcom/prisma";
import { IS_GOOGLE_LOGIN_ENABLED } from "@server/lib/constants";
import { ssrInit } from "@server/lib/ssr";
export async function getServerSideProps(context: GetServerSidePropsContext) {
const { req, query } = context;
const session = await getServerSession({ req });
const ssr = await ssrInit(context);
const verifyJwt = (jwt: string) => {
const secret = new TextEncoder().encode(process.env.CALENDSO_ENCRYPTION_KEY);
@@ -91,7 +89,6 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
return {
props: {
csrfToken: await getCsrfToken(context),
trpcState: ssr.dehydrate(),
isGoogleLoginEnabled: IS_GOOGLE_LOGIN_ENABLED,
isSAMLLoginEnabled,
samlTenantID,
@@ -12,8 +12,6 @@ import prisma from "@calcom/prisma";
import { asStringOrNull } from "@lib/asStringOrNull";
import { ssrInit } from "@server/lib/ssr";
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
// get query params and typecast them to string
// (would be even better to assert them instead of typecasting)
@@ -28,7 +26,7 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
const { req } = context;
const session = await getServerSession({ req });
const ssr = await ssrInit(context);
const { currentOrgDomain } = orgDomainConfig(context.req);
if (session) {
@@ -93,7 +91,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
return {
props: {
trpcState: ssr.dehydrate(),
provider: providerParam,
isSAMLLoginEnabled,
hostedCal,
@@ -5,12 +5,9 @@ import { getDeploymentKey } from "@calcom/features/ee/deployment/lib/getDeployme
import prisma from "@calcom/prisma";
import { UserPermissionRole } from "@calcom/prisma/enums";
import { ssrInit } from "@server/lib/ssr";
export async function getServerSideProps(context: GetServerSidePropsContext) {
const { req } = context;
const ssr = await ssrInit(context);
const userCount = await prisma.user.count();
const session = await getServerSession({ req });
@@ -45,7 +42,6 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
return {
props: {
trpcState: ssr.dehydrate(),
isFreeLicense,
userCount,
},
-72
View File
@@ -1,72 +0,0 @@
import type { GetServerSidePropsContext } from "next";
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 { createContext } from "@calcom/trpc/server/createContext";
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 { meRouter } from "@calcom/trpc/server/routers/viewer/me/_router";
import { hasTeamPlan } from "@calcom/trpc/server/routers/viewer/teams/procedures/hasTeamPlan";
import { mergeRouters, router } from "@calcom/trpc/server/trpc";
import { createServerSideHelpers } from "@trpc/react-query/server";
// Temporary workaround for OOM issue, import only procedures that are called on the server side
const routerSlice = router({
viewer: mergeRouters(
router({
me: meRouter,
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.
* 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.
*/
export async function ssrInit(context: GetServerSidePropsContext, options?: { noI18nPreload: boolean }) {
const ctx = await createContext(context);
const locale = await getLocale(context.req);
const ssr = createServerSideHelpers({
router: routerSlice,
transformer: superjson,
ctx: { ...ctx, locale },
});
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(),
ssr.viewer.me.get.prefetch(),
]);
return ssr;
}
@@ -1,9 +1,4 @@
import type {
AppGetServerSidePropsContext,
AppPrisma,
AppSsrInit,
AppUser,
} from "@calcom/types/AppGetServerSideProps";
import type { AppGetServerSidePropsContext, AppPrisma, AppUser } from "@calcom/types/AppGetServerSideProps";
import { enrichFormWithMigrationData } from "../enrichFormWithMigrationData";
import { getSerializableForm } from "../lib/getSerializableForm";
@@ -11,11 +6,8 @@ import { getSerializableForm } from "../lib/getSerializableForm";
export const getServerSidePropsForSingleFormView = async function getServerSidePropsForSingleFormView(
context: AppGetServerSidePropsContext,
prisma: AppPrisma,
user: AppUser,
ssrInit: AppSsrInit
user: AppUser
) {
const ssr = await ssrInit(context);
if (!user) {
return {
redirect: {
@@ -1,16 +1,9 @@
import { getTeamsFiltersFromQuery } from "@calcom/features/filters/lib/getTeamsFiltersFromQuery";
import type {
AppGetServerSidePropsContext,
AppPrisma,
AppSsrInit,
AppUser,
} from "@calcom/types/AppGetServerSideProps";
import type { AppGetServerSidePropsContext, AppPrisma, AppUser } from "@calcom/types/AppGetServerSideProps";
export const getServerSideProps = async function getServerSideProps(
context: AppGetServerSidePropsContext,
prisma: AppPrisma,
user: AppUser,
ssrInit: AppSsrInit
user: AppUser
) {
if (!user) {
return {
@@ -20,15 +13,7 @@ export const getServerSideProps = async function getServerSideProps(
},
};
}
const ssr = await ssrInit(context);
const filters = getTeamsFiltersFromQuery(context.query);
await ssr.viewer.appRoutingForms.forms.prefetch({
filters,
});
// Prefetch this so that New Button is immediately available
await ssr.viewer.teamsAndUserProfilesQuery.prefetch();
return {
props: {},
};
@@ -8,8 +8,6 @@ import { paymentDataSelect } from "@calcom/prisma/selects/payment";
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import type { inferSSRProps } from "@calcom/types/inferSSRProps";
import { ssrInit } from "../../../../../apps/web/server/lib/ssr";
export type PaymentPageProps = inferSSRProps<typeof getServerSideProps>;
const querySchema = z.object({
@@ -17,8 +15,6 @@ const querySchema = z.object({
});
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
const ssr = await ssrInit(context);
const { uid } = querySchema.parse(context.query);
const rawPayment = await prisma.payment.findFirst({
where: {
@@ -79,7 +75,6 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
metadata: EventTypeMetaDataSchema.parse(eventType.metadata),
},
booking,
trpcState: ssr.dehydrate(),
payment,
clientSecret: getClientSecretFromPayment(payment),
profile,
@@ -23,7 +23,7 @@ export const useAtomsEventTypePaymentInfo = ({
return useQuery({
queryKey: [QUERY_KEY, uid],
queryFn: () => {
return http?.get<ApiResponse<Omit<PaymentPageProps, "trpcState">>>(pathname).then((res) => {
return http?.get<ApiResponse<PaymentPageProps>>(pathname).then((res) => {
if (res.data.status === SUCCESS_STATUS) {
onEventTypePaymentInfoSuccess?.();
return res.data.data;
@@ -26,8 +26,8 @@ export const PaymentForm = ({
onEventTypePaymentInfoFailure,
}: {
paymentUid: string;
onPaymentSuccess?: (input: Omit<PaymentPageProps, "trpcState">) => void;
onPaymentCancellation?: (input: Omit<PaymentPageProps, "trpcState">) => void;
onPaymentSuccess?: (input: PaymentPageProps) => void;
onPaymentCancellation?: (input: PaymentPageProps) => void;
onEventTypePaymentInfoSuccess?: () => void;
onEventTypePaymentInfoFailure?: () => void;
}) => {
@@ -11,8 +11,8 @@ import { useLocale } from "@calcom/lib/hooks/useLocale";
const StripePaymentComponent = (
props: Props & {
onPaymentSuccess?: (input: Omit<PaymentPageProps, "trpcState">) => void;
onPaymentCancellation?: (input: Omit<PaymentPageProps, "trpcState">) => void;
onPaymentSuccess?: (input: PaymentPageProps) => void;
onPaymentCancellation?: (input: PaymentPageProps) => void;
}
) => {
const { t } = useLocale();
@@ -82,7 +82,7 @@ const StripePaymentComponent = (
});
} else {
setState({ status: "idle" });
props.onPaymentSuccess?.(props as unknown as Omit<PaymentPageProps, "trpcState">);
props.onPaymentSuccess?.(props as unknown as PaymentPageProps);
if (props.location) {
if (props.location.includes("integration")) {
params.location = t("web_conferencing_details_to_follow");
@@ -93,7 +93,7 @@ const StripePaymentComponent = (
}
}}
onCancel={() => {
props.onPaymentCancellation?.(props as unknown as Omit<PaymentPageProps, "trpcState">);
props.onPaymentCancellation?.(props as unknown as PaymentPageProps);
}}
onPaymentElementChange={() => {
setState({ status: "idle" });
@@ -105,8 +105,8 @@ const StripePaymentComponent = (
const StripePaymentForm = (
props: Props & {
uid: string;
onPaymentSuccess?: (input: Omit<PaymentPageProps, "trpcState">) => void;
onPaymentCancellation?: (input: Omit<PaymentPageProps, "trpcState">) => void;
onPaymentSuccess?: (input: PaymentPageProps) => void;
onPaymentCancellation?: (input: PaymentPageProps) => void;
}
) => {
const stripePromise = getStripe(props.payment.data.stripe_publishable_key as any);
+2 -5
View File
@@ -3,17 +3,14 @@ import type { CalendsoSessionUser } from "next-auth";
import type prisma from "@calcom/prisma";
import type { ssrInit } from "@server/lib/ssr";
export type AppUser = CalendsoSessionUser | undefined;
export type AppPrisma = typeof prisma;
export type AppGetServerSidePropsContext = GetServerSidePropsContext<{
pages: string[];
}>;
export type AppSsrInit = ssrInit;
export type AppGetServerSideProps = (
context: AppGetServerSidePropsContext,
prisma: AppPrisma,
user: AppUser,
ssrInit: AppSsrInit
user: AppUser
) => GetServerSidePropsResult;