@@ -21,8 +21,10 @@ import {
2121 hexToRgb ,
2222} from '@gitbook/colors' ;
2323import { IconStyle , IconsProvider } from '@gitbook/icons' ;
24+ import * as ReactDOM from 'react-dom' ;
2425
25- import { fontNotoColorEmoji , fonts , ibmPlexMono } from '@/fonts' ;
26+ import { getFontData } from '@/fonts' ;
27+ import { fontNotoColorEmoji , ibmPlexMono } from '@/fonts/default' ;
2628import { getSpaceLanguage } from '@/intl/server' ;
2729import { getAssetURL } from '@/lib/assets' ;
2830import { tcls } from '@/lib/tailwind' ;
@@ -31,7 +33,7 @@ import { ClientContexts } from './ClientContexts';
3133
3234import '@gitbook/icons/style.css' ;
3335import './globals.css' ;
34- import { GITBOOK_ICONS_TOKEN , GITBOOK_ICONS_URL } from '@v2/lib/env' ;
36+ import { GITBOOK_FONTS_URL , GITBOOK_ICONS_TOKEN , GITBOOK_ICONS_URL } from '@v2/lib/env' ;
3537
3638/**
3739 * Layout shared between the content and the PDF renderer.
@@ -48,6 +50,22 @@ export async function CustomizationRootLayout(props: {
4850 const mixColor = getTintMixColor ( customization . styling . primaryColor , tintColor ) ;
4951 const sidebarStyles = getSidebarStyles ( customization ) ;
5052 const { infoColor, successColor, warningColor, dangerColor } = getSemanticColors ( customization ) ;
53+ const fontData = getFontData ( customization . styling . font ) ;
54+
55+ // Preconnect and preload custom fonts if needed
56+ if ( fontData . type === 'custom' ) {
57+ ReactDOM . preconnect ( GITBOOK_FONTS_URL ) ;
58+ fontData . preloadSources
59+ . flatMap ( ( face ) => face . sources )
60+ . forEach ( ( { url, format } ) => {
61+ ReactDOM . preload ( url , {
62+ as : 'font' ,
63+ crossOrigin : 'anonymous' ,
64+ fetchPriority : 'high' ,
65+ type : format ? `font/${ format } ` : undefined ,
66+ } ) ;
67+ } ) ;
68+ }
5169
5270 return (
5371 < html
@@ -66,16 +84,18 @@ export async function CustomizationRootLayout(props: {
6684 sidebarStyles . list && `sidebar-list-${ sidebarStyles . list } ` ,
6785 'links' in customization . styling && `links-${ customization . styling . links } ` ,
6886 fontNotoColorEmoji . variable ,
69- typeof customization . styling . font === 'string'
70- ? fonts [ customization . styling . font ] . variable
71- : '' ,
72- ibmPlexMono . variable
87+ ibmPlexMono . variable ,
88+ fontData . type === 'default' ? fontData . variable : 'font-custom'
7389 ) }
7490 >
7591 < head >
7692 { customization . privacyPolicy . url ? (
7793 < link rel = "privacy-policy" href = { customization . privacyPolicy . url } />
7894 ) : null }
95+
96+ { /* Inject custom font @font-face rules */ }
97+ { fontData . type === 'custom' ? < style > { fontData . fontFaceRules } </ style > : null }
98+
7999 < style
80100 nonce = {
81101 //Since I can't get the nonce to work for inline styles, we need to allow unsafe-inline
0 commit comments