Skip to content

Commit ac11ba2

Browse files
authored
feat: add font preloading to reduce fout (#545)
1 parent 96b22ae commit ac11ba2

File tree

8 files changed

+75
-1
lines changed

8 files changed

+75
-1
lines changed

antora-ui/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/build/
22
/node_modules/
33
/public/
4-
/src/css/boostlook.css
4+
# /src/css/boostlook.css
55

1.62 MB
Binary file not shown.
732 KB
Binary file not shown.
1.47 MB
Binary file not shown.
667 KB
Binary file not shown.
1.63 MB
Binary file not shown.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<style>html.fonts-loading{visibility:hidden;opacity:0}</style>
2+
<script>document.documentElement.classList.add('fonts-loading');</script>
3+
4+
<link rel="preload" href="{{{uiRootPath}}}/font/NotoSansDisplay.woff2" as="font" type="font/woff2" crossorigin="anonymous" />
5+
<link rel="preload" href="{{{uiRootPath}}}/font/NotoSansDisplay-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous" />
6+
<link rel="preload" href="{{{uiRootPath}}}/font/MonaspaceNeon-Var.woff2" as="font" type="font/woff2" crossorigin="anonymous" />
7+
<link rel="preload" href="{{{uiRootPath}}}/font/MonaspaceXenon-Var.woff2" as="font" type="font/woff2" crossorigin="anonymous" />
8+
9+
<script>
10+
(function() {
11+
'use strict';
12+
13+
var revealed = false;
14+
15+
var reveal = function() {
16+
if (revealed) return;
17+
revealed = true;
18+
document.documentElement.classList.remove('fonts-loading');
19+
};
20+
21+
setTimeout(reveal, 3000);
22+
23+
if (!('FontFace' in window) || !('fonts' in document)) {
24+
setTimeout(reveal, 100);
25+
return;
26+
}
27+
28+
var uiRoot = '{{{uiRootPath}}}';
29+
var fonts = [
30+
{
31+
family: 'Noto Sans',
32+
url: uiRoot + '/font/NotoSansDisplay.woff2',
33+
descriptors: { style: 'normal', weight: '100 900', stretch: '62.5% 100%' }
34+
},
35+
{
36+
family: 'Noto Sans',
37+
url: uiRoot + '/font/NotoSansDisplay-Italic.woff2',
38+
descriptors: { style: 'italic', weight: '100 900', stretch: '62.5% 100%' }
39+
},
40+
{
41+
family: 'Monaspace Neon',
42+
url: uiRoot + '/font/MonaspaceNeon-Var.woff2',
43+
descriptors: { style: 'normal', weight: '400' }
44+
},
45+
{
46+
family: 'Monaspace Xenon',
47+
url: uiRoot + '/font/MonaspaceXenon-Var.woff2',
48+
descriptors: { style: 'italic', weight: '400' }
49+
}
50+
];
51+
52+
var loadPromises = fonts.map(function(f) {
53+
try {
54+
var face = new FontFace(f.family, 'url("' + f.url + '")', f.descriptors);
55+
return face.load().then(function(loaded) {
56+
document.fonts.add(loaded);
57+
return loaded;
58+
}).catch(function() {
59+
return null;
60+
});
61+
} catch (e) {
62+
return Promise.resolve(null);
63+
}
64+
});
65+
66+
Promise.all(loadPromises)
67+
.then(function() {
68+
return document.fonts.ready;
69+
})
70+
.then(reveal)
71+
.catch(reveal);
72+
})();
73+
</script>

antora-ui/src/partials/head.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{> head-prelude}}
2+
{{> head-fonts}}
23
{{> head-title}}
34
{{> head-info}}
45
{{> head-styles}}

0 commit comments

Comments
 (0)