perf: Cache translations per language/per release (#10843)
Co-authored-by: zomars <[email protected]>
This commit is contained in:
1 parent
e8554ed5c5
commit
3f273235a6
18 files changed
+75
-80
No files matched your search
@@ -73,6 +73,7 @@ export async function getServerSession(options: {
|
||||
impersonatedByUID: token.impersonatedByUID ?? undefined,
|
||||
belongsToActiveTeam: token.belongsToActiveTeam,
|
||||
organizationId: token.organizationId,
|
||||
locale: user.locale ?? undefined,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { UserPermissionRole, Membership, Team } from "@prisma/client";
|
||||
import type { Membership, Team, UserPermissionRole } from "@prisma/client";
|
||||
import type { AuthOptions, Session } from "next-auth";
|
||||
import { encode } from "next-auth/jwt";
|
||||
import type { Provider } from "next-auth/providers";
|
||||
@@ -85,6 +85,7 @@ const providers: Provider[] = [
|
||||
organizationId: true,
|
||||
twoFactorEnabled: true,
|
||||
twoFactorSecret: true,
|
||||
locale: true,
|
||||
organization: {
|
||||
select: {
|
||||
id: true,
|
||||
@@ -181,6 +182,7 @@ const providers: Provider[] = [
|
||||
role: validateRole(user.role),
|
||||
belongsToActiveTeam: hasActiveTeams,
|
||||
organizationId: user.organizationId,
|
||||
locale: user.locale,
|
||||
};
|
||||
},
|
||||
}),
|
||||
@@ -225,6 +227,7 @@ if (isSAMLLoginEnabled) {
|
||||
email: profile.email || "",
|
||||
name: `${profile.firstName || ""} ${profile.lastName || ""}`.trim(),
|
||||
email_verified: true,
|
||||
locale: profile.locale,
|
||||
};
|
||||
},
|
||||
options: {
|
||||
@@ -356,6 +359,7 @@ export const AUTH_OPTIONS: AuthOptions = {
|
||||
if (trigger === "update") {
|
||||
return {
|
||||
...token,
|
||||
locale: session?.locale ?? token.locale,
|
||||
name: session?.name ?? token.name,
|
||||
username: session?.username ?? token.username,
|
||||
email: session?.email ?? token.email,
|
||||
@@ -372,6 +376,7 @@ export const AUTH_OPTIONS: AuthOptions = {
|
||||
email: true,
|
||||
organizationId: true,
|
||||
role: true,
|
||||
locale: true,
|
||||
teams: {
|
||||
include: {
|
||||
team: true,
|
||||
@@ -386,7 +391,7 @@ export const AUTH_OPTIONS: AuthOptions = {
|
||||
|
||||
// Check if the existingUser has any active teams
|
||||
const belongsToActiveTeam = checkIfUserBelongsToActiveTeam(existingUser);
|
||||
const { teams, ...existingUserWithoutTeamsField } = existingUser;
|
||||
const { teams: _teams, ...existingUserWithoutTeamsField } = existingUser;
|
||||
|
||||
return {
|
||||
...existingUserWithoutTeamsField,
|
||||
@@ -416,6 +421,7 @@ export const AUTH_OPTIONS: AuthOptions = {
|
||||
impersonatedByUID: user?.impersonatedByUID,
|
||||
belongsToActiveTeam: user?.belongsToActiveTeam,
|
||||
organizationId: user?.organizationId,
|
||||
locale: user?.locale,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -454,6 +460,7 @@ export const AUTH_OPTIONS: AuthOptions = {
|
||||
impersonatedByUID: token.impersonatedByUID as number,
|
||||
belongsToActiveTeam: token?.belongsToActiveTeam as boolean,
|
||||
organizationId: token?.organizationId,
|
||||
locale: existingUser.locale,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -473,6 +480,7 @@ export const AUTH_OPTIONS: AuthOptions = {
|
||||
impersonatedByUID: token.impersonatedByUID as number,
|
||||
belongsToActiveTeam: token?.belongsToActiveTeam as boolean,
|
||||
organizationId: token?.organizationId,
|
||||
locale: token.locale,
|
||||
},
|
||||
};
|
||||
return calendsoSession;
|
||||
|
||||
@@ -11,7 +11,7 @@ const teamIdschema = z.object({
|
||||
});
|
||||
|
||||
const auditAndReturnNextUser = async (
|
||||
impersonatedUser: Pick<User, "id" | "username" | "email" | "name" | "role" | "organizationId">,
|
||||
impersonatedUser: Pick<User, "id" | "username" | "email" | "name" | "role" | "organizationId" | "locale">,
|
||||
impersonatedByUID: number,
|
||||
hasTeam?: boolean
|
||||
) => {
|
||||
@@ -40,6 +40,7 @@ const auditAndReturnNextUser = async (
|
||||
impersonatedByUID,
|
||||
belongsToActiveTeam: hasTeam,
|
||||
organizationId: impersonatedUser.organizationId,
|
||||
locale: impersonatedUser.locale,
|
||||
};
|
||||
|
||||
return obj;
|
||||
@@ -97,6 +98,7 @@ const ImpersonationProvider = CredentialsProvider({
|
||||
email: true,
|
||||
organizationId: true,
|
||||
disableImpersonation: true,
|
||||
locale: true,
|
||||
teams: {
|
||||
where: {
|
||||
disableImpersonation: false, // Ensure they have impersonation enabled
|
||||
|
||||
@@ -23,9 +23,6 @@ const DisableTeamImpersonation = ({
|
||||
showToast(t("your_user_profile_updated_successfully"), "success");
|
||||
await utils.viewer.teams.getMembershipbyUser.invalidate();
|
||||
},
|
||||
async onSettled() {
|
||||
await utils.viewer.public.i18n.invalidate();
|
||||
},
|
||||
});
|
||||
if (query.isLoading) return <></>;
|
||||
|
||||
|
||||
@@ -59,11 +59,12 @@ export function createNextApiHandler(router: AnyRouter, isPublic = false, namesp
|
||||
if (isPublic && paths) {
|
||||
const ONE_DAY_IN_SECONDS = 60 * 60 * 24;
|
||||
const FIVE_MINUTES_IN_SECONDS = 5 * 60;
|
||||
const ONE_YEAR_IN_SECONDS = 31536000;
|
||||
|
||||
const cacheRules = {
|
||||
session: "no-cache",
|
||||
|
||||
// i18n data is user specific and thus should not be cached
|
||||
i18n: "no-cache",
|
||||
i18n: `max-age=${ONE_YEAR_IN_SECONDS}`,
|
||||
|
||||
// FIXME: Using `max-age=1, stale-while-revalidate=60` fails some booking tests.
|
||||
"slots.getSchedule": `no-cache`,
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import sessionMiddleware from "./sessionMiddleware";
|
||||
|
||||
const localeMiddleware = sessionMiddleware.unstable_pipe(async ({ ctx, next }) => {
|
||||
const { serverSideTranslations } = await import("next-i18next/serverSideTranslations");
|
||||
const { user } = ctx;
|
||||
|
||||
const i18n = await serverSideTranslations(
|
||||
user?.locale && user?.locale !== ctx.locale ? user.locale : ctx.locale,
|
||||
["common", "vital"]
|
||||
);
|
||||
|
||||
const locale = user?.locale || ctx.locale;
|
||||
|
||||
return next({
|
||||
ctx: { locale, i18n, user: { ...user, locale } },
|
||||
});
|
||||
});
|
||||
|
||||
export default localeMiddleware;
|
||||
@@ -1,9 +1,9 @@
|
||||
import localeMiddleware from "../../middlewares/localeMiddleware";
|
||||
import sessionMiddleware from "../../middlewares/sessionMiddleware";
|
||||
import publicProcedure from "../../procedures/publicProcedure";
|
||||
import { router } from "../../trpc";
|
||||
import { slotsRouter } from "../viewer/slots/_router";
|
||||
import { ZEventInputSchema } from "./event.schema";
|
||||
import { i18nInputSchema } from "./i18n.schema";
|
||||
import { ZSamlTenantProductInputSchema } from "./samlTenantProduct.schema";
|
||||
import { ZStripeCheckoutSessionInputSchema } from "./stripeCheckoutSession.schema";
|
||||
|
||||
@@ -36,7 +36,7 @@ export const publicViewerRouter = router({
|
||||
});
|
||||
}),
|
||||
|
||||
i18n: publicProcedure.use(localeMiddleware).query(async ({ ctx }) => {
|
||||
i18n: publicProcedure.input(i18nInputSchema).query(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.i18n) {
|
||||
UNSTABLE_HANDLER_CACHE.i18n = await import("./i18n.handler").then((mod) => mod.i18nHandler);
|
||||
}
|
||||
@@ -46,7 +46,7 @@ export const publicViewerRouter = router({
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.i18n({ ctx });
|
||||
return UNSTABLE_HANDLER_CACHE.i18n({ ctx, input });
|
||||
}),
|
||||
|
||||
countryCode: publicProcedure.query(async ({ ctx }) => {
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import type { WithLocale } from "../../createContext";
|
||||
import type { I18nInputSchema } from "./i18n.schema";
|
||||
|
||||
type I18nOptions = {
|
||||
ctx: WithLocale & {
|
||||
req: NextApiRequest | undefined;
|
||||
res: NextApiResponse | undefined;
|
||||
};
|
||||
input: I18nInputSchema;
|
||||
};
|
||||
|
||||
export const i18nHandler = async ({ ctx }: I18nOptions) => {
|
||||
const { locale, i18n } = ctx;
|
||||
export const i18nHandler = async ({ input }: I18nOptions) => {
|
||||
const { locale } = input;
|
||||
const { serverSideTranslations } = await import("next-i18next/serverSideTranslations");
|
||||
const i18n = await serverSideTranslations(locale, ["common", "vital"]);
|
||||
|
||||
return {
|
||||
i18n,
|
||||
|
||||
@@ -1 +1,8 @@
|
||||
export {};
|
||||
import { z } from "zod";
|
||||
|
||||
export const i18nInputSchema = z.object({
|
||||
locale: z.string(),
|
||||
CalComVersion: z.string(),
|
||||
});
|
||||
|
||||
export type I18nInputSchema = z.infer<typeof i18nInputSchema>;
|
||||
Vendored
+2
@@ -19,6 +19,7 @@ declare module "next-auth" {
|
||||
organizationId?: number | null;
|
||||
username?: PrismaUser["username"];
|
||||
role?: PrismaUser["role"] | "INACTIVE_ADMIN";
|
||||
locale?: string | null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,5 +33,6 @@ declare module "next-auth/jwt" {
|
||||
impersonatedByUID?: number | null;
|
||||
belongsToActiveTeam?: boolean;
|
||||
organizationId?: number | null;
|
||||
locale?: string;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user