@@ -21,8 +21,10 @@ import {
21
21
hexToRgb ,
22
22
} from '@gitbook/colors' ;
23
23
import { IconStyle , IconsProvider } from '@gitbook/icons' ;
24
+ import * as ReactDOM from 'react-dom' ;
24
25
25
- import { fontNotoColorEmoji , fonts , ibmPlexMono } from '@/fonts' ;
26
+ import { getFontData } from '@/fonts' ;
27
+ import { fontNotoColorEmoji , ibmPlexMono } from '@/fonts/default' ;
26
28
import { getSpaceLanguage } from '@/intl/server' ;
27
29
import { getAssetURL } from '@/lib/assets' ;
28
30
import { tcls } from '@/lib/tailwind' ;
@@ -31,7 +33,7 @@ import { ClientContexts } from './ClientContexts';
31
33
32
34
import '@gitbook/icons/style.css' ;
33
35
import './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' ;
35
37
36
38
/**
37
39
* Layout shared between the content and the PDF renderer.
@@ -48,6 +50,22 @@ export async function CustomizationRootLayout(props: {
48
50
const mixColor = getTintMixColor ( customization . styling . primaryColor , tintColor ) ;
49
51
const sidebarStyles = getSidebarStyles ( customization ) ;
50
52
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
+ }
51
69
52
70
return (
53
71
< html
@@ -66,16 +84,18 @@ export async function CustomizationRootLayout(props: {
66
84
sidebarStyles . list && `sidebar-list-${ sidebarStyles . list } ` ,
67
85
'links' in customization . styling && `links-${ customization . styling . links } ` ,
68
86
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'
73
89
) }
74
90
>
75
91
< head >
76
92
{ customization . privacyPolicy . url ? (
77
93
< link rel = "privacy-policy" href = { customization . privacyPolicy . url } />
78
94
) : null }
95
+
96
+ { /* Inject custom font @font-face rules */ }
97
+ { fontData . type === 'custom' ? < style > { fontData . fontFaceRules } </ style > : null }
98
+
79
99
< style
80
100
nonce = {
81
101
//Since I can't get the nonce to work for inline styles, we need to allow unsafe-inline
0 commit comments