From 3fb6ac3ca4bdef5e9ffb3c08bfdbce028d83f10e Mon Sep 17 00:00:00 2001 From: Alessio Gravili Date: Wed, 15 Jan 2025 13:22:32 -0700 Subject: [PATCH] perf: ensure unnecessary config translations are not sent to the client (#10524) This will reduce the size of the initial HTML --- packages/next/src/layouts/Root/index.tsx | 25 +++++++++---------- packages/payload/src/config/client.ts | 9 ++----- packages/ui/src/providers/Root/index.tsx | 4 +-- .../ui/src/providers/Translation/index.tsx | 5 ++-- .../collections/Lexical/e2e/main/e2e.spec.ts | 12 +++++++++ 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/packages/next/src/layouts/Root/index.tsx b/packages/next/src/layouts/Root/index.tsx index cc4f7f0ca..8468a4264 100644 --- a/packages/next/src/layouts/Root/index.tsx +++ b/packages/next/src/layouts/Root/index.tsx @@ -1,5 +1,5 @@ import type { AcceptedLanguages } from '@payloadcms/translations' -import type { ImportMap, SanitizedConfig, ServerFunctionClient } from 'payload' +import type { ImportMap, LanguageOptions, SanitizedConfig, ServerFunctionClient } from 'payload' import { rtlLanguages } from '@payloadcms/translations' import { RootProvider } from '@payloadcms/ui' @@ -60,19 +60,18 @@ export const RootLayout = async ({ ? 'RTL' : 'LTR' - const languageOptions = Object.entries(config.i18n.supportedLanguages || {}).reduce( - (acc, [language, languageConfig]) => { - if (Object.keys(config.i18n.supportedLanguages).includes(language)) { - acc.push({ - label: languageConfig.translations.general.thisLanguage, - value: language, - }) - } + const languageOptions: LanguageOptions = Object.entries( + config.i18n.supportedLanguages || {}, + ).reduce((acc, [language, languageConfig]) => { + if (Object.keys(config.i18n.supportedLanguages).includes(language)) { + acc.push({ + label: languageConfig.translations.general.thisLanguage, + value: language, + }) + } - return acc - }, - [], - ) + return acc + }, []) async function switchLanguageServerAction(lang: string): Promise { 'use server' diff --git a/packages/payload/src/config/client.ts b/packages/payload/src/config/client.ts index 227108c71..b0e6a74cf 100644 --- a/packages/payload/src/config/client.ts +++ b/packages/payload/src/config/client.ts @@ -26,6 +26,7 @@ export type ServerOnlyRootProperties = keyof Pick< | 'endpoints' | 'graphQL' | 'hooks' + | 'i18n' | 'jobs' | 'logger' | 'onInit' @@ -44,7 +45,6 @@ export type ClientConfig = { collections: ClientCollectionConfig[] custom?: Record globals: ClientGlobalConfig[] - i18n?: Omit } & Omit export const serverOnlyAdminConfigProperties: readonly Partial[] = [] @@ -59,6 +59,7 @@ export const serverOnlyConfigProperties: readonly Partial>({ type Props = { children: React.ReactNode dateFNSKey: Language['dateFNSKey'] - fallbackLang: ClientConfig['i18n']['fallbackLanguage'] + fallbackLang: I18nOptions['fallbackLanguage'] language: string languageOptions: LanguageOptions switchLanguageServerAction: (lang: string) => Promise diff --git a/test/fields/collections/Lexical/e2e/main/e2e.spec.ts b/test/fields/collections/Lexical/e2e/main/e2e.spec.ts index 1865f76c5..250b4c44f 100644 --- a/test/fields/collections/Lexical/e2e/main/e2e.spec.ts +++ b/test/fields/collections/Lexical/e2e/main/e2e.spec.ts @@ -1278,6 +1278,18 @@ describe('lexicalMain', () => { }) describe('localization', () => { + test('ensure lexical translations from other languages do not get sent to the client', async () => { + await navigateToLexicalFields() + // Now check if the html contains "Comience a escribir" + + const htmlContent = await page.content() + + // Check if the HTML contains "Comience a escribir" + expect(htmlContent).not.toContain('Comience a escribir') + expect(htmlContent).not.toContain('Beginne zu tippen oder') + expect(htmlContent).not.toContain('Cargando...') + expect(htmlContent).toContain('Start typing, or press') + }) test.skip('ensure simple localized lexical field works', async () => { await navigateToLexicalFields(true, true) })