refactor: implementation of locale calculated server-side (#11534)

This commit is contained in:
Greg Pabian authored and GitHub committed 2023-10-10 09:36:28 -07:00
1 parent b4c6388ce0
commit 778485b31d
18 files changed
+139 -120

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
import { parse } from "accept-language-parser";
import type { GetTokenParams } from "next-auth/jwt";
import { getToken } from "next-auth/jwt";
/**
* This is a slimmed down version of the `getServerSession` function from
* `next-auth`.
*
* Instead of requiring the entire options object for NextAuth, we create
* a compatible session using information from the incoming token.
*
* The downside to this is that we won't refresh sessions if the users
* token has expired (30 days). This should be fine as we call `/auth/session`
* frequently enough on the client-side to keep the session alive.
*/
export const getLocale = async (req: GetTokenParams["req"]): Promise<string> => {
const token = await getToken({
req,
});
const tokenLocale = token?.["locale"];
if (tokenLocale !== undefined) {
return tokenLocale;
}
const acceptLanguage =
req.headers instanceof Headers ? req.headers.get("accept-language") : req.headers["accept-language"];
const languages = acceptLanguage ? parse(acceptLanguage) : [];
return languages[0]?.code ?? "en";
};
-22
View File
@@ -1,22 +0,0 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import parser from "accept-language-parser";
import type { GetServerSidePropsContext, NextApiRequest } from "next";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import type { Maybe } from "@calcom/trpc/server";
const { i18n } = require("@calcom/config/next-i18next.config");
export async function getLocaleFromRequest(
req: NextApiRequest | GetServerSidePropsContext["req"]
): Promise<string> {
const session = await getServerSession({ req });
if (session?.user?.locale) return session.user.locale;
let preferredLocale: string | null | undefined;
if (req.headers["accept-language"]) {
preferredLocale = parser.pick(i18n.locales, req.headers["accept-language"], {
loose: true,
}) as Maybe<string>;
}
return preferredLocale ?? i18n.defaultLocale;
}
+2 -2
View File
@@ -3,7 +3,7 @@ import type { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from
import type { Session } from "next-auth";
import type { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { getLocaleFromRequest } from "@calcom/lib/getLocaleFromRequest";
import { getLocale } from "@calcom/features/auth/lib/getLocale";
import prisma from "@calcom/prisma";
import type { SelectedCalendar, User as PrismaUser } from "@calcom/prisma/client";
@@ -62,7 +62,7 @@ export async function createContextInner(opts: CreateInnerContextOptions) {
* @link https://trpc.io/docs/context
*/
export const createContext = async ({ req, res }: CreateContextOptions, sessionGetter?: GetSessionFn) => {
const locale = await getLocaleFromRequest(req);
const locale = await getLocale(req);
const session = !!sessionGetter ? await sessionGetter({ req, res }) : null;
const contextInner = await createContextInner({ locale, session });
return {
@@ -92,7 +92,7 @@ export async function getUserFromSession(ctx: TRPCContextInner, session: Maybe<S
const orgMetadata = teamMetadataSchema.parse(user.organization?.metadata || {});
// This helps to prevent reaching the 4MB payload limit by avoiding base64 and instead passing the avatar url
const locale = user?.locale || ctx.locale;
const locale = user?.locale ?? ctx.locale;
const isOrgAdmin = !!user.organization?.members.length;
// Want to reduce the amount of data being sent
@@ -1,13 +1,6 @@
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;
};