diff --git a/packages/docusaurus-theme-classic/src/__tests__/translations.test.ts b/packages/docusaurus-theme-classic/src/__tests__/translations.test.ts index 8bde386a0421..5d761c50b10f 100644 --- a/packages/docusaurus-theme-classic/src/__tests__/translations.test.ts +++ b/packages/docusaurus-theme-classic/src/__tests__/translations.test.ts @@ -87,3 +87,24 @@ describe('translateThemeConfig', () => { ).toMatchSnapshot(); }); }); + +describe('getTranslationFiles and translateThemeConfig isomorphism', () => { + function verifyIsomorphism(themeConfig: ThemeConfig) { + const translationFiles = getTranslationFiles({themeConfig}); + const translatedThemeConfig = translateThemeConfig({ + themeConfig, + translationFiles, + }); + expect(translatedThemeConfig).toEqual(themeConfig); + } + + test('should be verified for main sample', () => { + verifyIsomorphism(ThemeConfigSample); + }); + + // undefined footer should not make the translation code crash + // See https://github.com/facebook/docusaurus/issues/3936 + test('should be verified for sample without footer', () => { + verifyIsomorphism({...ThemeConfigSample, footer: undefined}); + }); +}); diff --git a/packages/docusaurus-theme-classic/src/translations.ts b/packages/docusaurus-theme-classic/src/translations.ts index 1c30062b241c..dec451cd03d8 100644 --- a/packages/docusaurus-theme-classic/src/translations.ts +++ b/packages/docusaurus-theme-classic/src/translations.ts @@ -129,10 +129,17 @@ export function getTranslationFiles({ }: { themeConfig: ThemeConfig; }): TranslationFile[] { - return [ + const translationFiles: (TranslationFile | undefined)[] = [ {path: 'navbar', content: getNavbarTranslationFile(themeConfig.navbar)}, - {path: 'footer', content: getFooterTranslationFile(themeConfig.footer)}, + themeConfig.footer + ? { + path: 'footer', + content: getFooterTranslationFile(themeConfig.footer), + } + : undefined, ]; + + return translationFiles.filter(Boolean) as TranslationFile[]; } export function translateThemeConfig({ @@ -153,9 +160,8 @@ export function translateThemeConfig({ themeConfig.navbar, translationFilesMap.navbar.content, ), - footer: translateFooter( - themeConfig.footer, - translationFilesMap.footer.content, - ), + footer: themeConfig.footer + ? translateFooter(themeConfig.footer, translationFilesMap.footer.content) + : undefined, }; } diff --git a/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts b/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts index 394f4b32f65d..81356601e4c8 100644 --- a/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts +++ b/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts @@ -66,7 +66,7 @@ export type ThemeConfig = { colorMode: any; announcementBar: any; prism: any; - footer: Footer; + footer: Footer | undefined; hideableSidebar: any; };