perf: make useLocale hook to use server-fetched i18n if in App router context (#20597)
* write I18nProvider * rename * update useLocale * fix * use a singleton * safer * better * fix e2e test 1 * fix e2e test 2 * fix e2e test 3 * fix e2e test 4 * fix * refactor * address comments * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix flaky test * add more caching * revert changes to e2e tests * add jsdoc * memoize * update window object with new translations in /settings/my-account/general * remove dead code * only use locale and ns as deps * fix
This commit is contained in:
1 parent
9eb1fe06e3
commit
b17b5a169f
7 files changed
+182
-101
No files matched your search
@@ -0,0 +1,33 @@
|
||||
"use client";
|
||||
|
||||
import { createContext, useMemo } from "react";
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
type AppRouterI18nContextType = {
|
||||
translations: Record<string, string>;
|
||||
ns: string;
|
||||
locale: string;
|
||||
};
|
||||
|
||||
export const AppRouterI18nContext = createContext<AppRouterI18nContextType | null>(null);
|
||||
|
||||
export function AppRouterI18nProvider({
|
||||
children,
|
||||
translations,
|
||||
locale,
|
||||
ns,
|
||||
}: AppRouterI18nContextType & {
|
||||
children: ReactNode;
|
||||
}) {
|
||||
// Memoize the value to prevent re-renders unless the data changes
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
translations,
|
||||
locale,
|
||||
ns,
|
||||
}),
|
||||
[locale, ns]
|
||||
);
|
||||
|
||||
return <AppRouterI18nContext.Provider value={value}>{children}</AppRouterI18nContext.Provider>;
|
||||
}
|
||||
+17
-3
@@ -5,10 +5,12 @@ import { headers, cookies } from "next/headers";
|
||||
import React from "react";
|
||||
|
||||
import { getLocale } from "@calcom/features/auth/lib/getLocale";
|
||||
import { loadTranslations } from "@calcom/lib/server/i18n";
|
||||
import { IconSprites } from "@calcom/ui/components/icon";
|
||||
import { NotificationSoundHandler } from "@calcom/web/components/notification-sound-handler";
|
||||
|
||||
import "../styles/globals.css";
|
||||
import { AppRouterI18nProvider } from "./AppRouterI18nProvider";
|
||||
import { SpeculationRules } from "./SpeculationRules";
|
||||
import { Providers } from "./providers";
|
||||
|
||||
@@ -83,10 +85,15 @@ const getInitialProps = async (url: string) => {
|
||||
const embedColorScheme = searchParams?.get("ui.color-scheme");
|
||||
|
||||
const req = { headers: await headers(), cookies: await cookies() };
|
||||
const newLocale = await getLocale(req);
|
||||
const newLocale = (await getLocale(req)) ?? "en";
|
||||
const direction = dir(newLocale);
|
||||
|
||||
return { isEmbed, embedColorScheme, locale: newLocale, direction };
|
||||
return {
|
||||
isEmbed,
|
||||
embedColorScheme,
|
||||
locale: newLocale,
|
||||
direction,
|
||||
};
|
||||
};
|
||||
|
||||
const getFallbackProps = () => ({
|
||||
@@ -107,6 +114,8 @@ export default async function RootLayout({ children }: { children: React.ReactNo
|
||||
const { locale, direction, isEmbed, embedColorScheme } = isSSG
|
||||
? getFallbackProps()
|
||||
: await getInitialProps(fullUrl);
|
||||
const ns = "common";
|
||||
const translations = await loadTranslations(locale, ns);
|
||||
|
||||
return (
|
||||
<html
|
||||
@@ -153,7 +162,12 @@ export default async function RootLayout({ children }: { children: React.ReactNo
|
||||
"/insights",
|
||||
]}
|
||||
/>
|
||||
<Providers>{children}</Providers>
|
||||
|
||||
<Providers>
|
||||
<AppRouterI18nProvider translations={translations} locale={locale} ns={ns}>
|
||||
{children}
|
||||
</AppRouterI18nProvider>
|
||||
</Providers>
|
||||
{!isEmbed && <NotificationSoundHandler />}
|
||||
<NotificationSoundHandler />
|
||||
</body>
|
||||
|
||||
Reference in new issue
Block a user