Skip to content

Commit 9471437

Browse files
committed
Hide content until fonts load to eliminate FOUT + FOIT
1 parent 16043c5 commit 9471437

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed
Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
<link rel="dns-prefetch" href="https://cppalliance.org">
22
<link rel="preconnect" href="https://cppalliance.org" crossorigin>
33
<link rel="preload" href="https://cppalliance.org/fonts/NotoSansDisplay.ttf" as="font" type="font/ttf" crossorigin fetchpriority="high" />
4-
<link rel="preload" href="https://cppalliance.org/fonts/NotoSansDisplay-Italic.ttf" as="font" type="font/ttf"
5-
crossorigin fetchpriority="high" />
6-
<link rel="preload" href="https://cppalliance.org/fonts/NotoSansMono.ttf" as="font" type="font/ttf" crossorigin fetchpriority="high" />
4+
<link rel="preload" href="https://cppalliance.org/fonts/NotoSansDisplay-Italic.ttf" as="font" type="font/ttf" crossorigin fetchpriority="high" />
5+
<link rel="preload" href="https://cppalliance.org/fonts/NotoSansMono.ttf" as="font" type="font/ttf" crossorigin fetchpriority="high" />
6+
<script>
7+
(function() {
8+
'use strict';
9+
if (!('fonts' in document)) return;
10+
11+
document.documentElement.style.visibility = 'hidden';
12+
13+
var fontUrls = [
14+
{ family: 'Noto Sans', url: 'https://cppalliance.org/fonts/NotoSansDisplay.ttf', style: 'normal' },
15+
{ family: 'Noto Sans', url: 'https://cppalliance.org/fonts/NotoSansDisplay-Italic.ttf', style: 'italic' },
16+
{ family: 'Noto Sans Mono', url: 'https://cppalliance.org/fonts/NotoSansMono.ttf', style: 'normal' }
17+
];
18+
19+
var fontPromises = fontUrls.map(function(fontConfig) {
20+
var fontFace = new FontFace(fontConfig.family, 'url(' + fontConfig.url + ')', {
21+
style: fontConfig.style
22+
});
23+
return fontFace.load().then(function(loadedFont) {
24+
document.fonts.add(loadedFont);
25+
return loadedFont;
26+
}).catch(function() {
27+
return null;
28+
});
29+
});
30+
31+
var showContent = function() {
32+
document.documentElement.style.visibility = '';
33+
};
34+
35+
Promise.all(fontPromises).then(function() {
36+
document.fonts.ready.then(showContent);
37+
}).catch(function() {
38+
setTimeout(showContent, 100);
39+
});
40+
})();
41+
</script>

0 commit comments

Comments
 (0)