Skip to content

Commit 81e3d1e

Browse files
committed
Refactor font loading script to improve FOUT handling
Update the font loading logic to add a 'fonts-loading' class to the HTML element, ensuring content remains hidden until fonts are fully loaded. Adjust timeout settings for better performance and error handling during font loading, enhancing user experience.
1 parent deba2dc commit 81e3d1e

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
2-
<style>html{visibility:hidden;opacity:0}</style>
1+
<style>html.fonts-loading{visibility:hidden;opacity:0}</style>
2+
<script>document.documentElement.classList.add('fonts-loading');</script>
33

44
<link rel="preload" href="{{{uiRootPath}}}/font/NotoSansDisplay.woff2" as="font" type="font/woff2" crossorigin="anonymous" />
55
<link rel="preload" href="{{{uiRootPath}}}/font/NotoSansDisplay-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous" />
@@ -10,27 +10,21 @@
1010
(function() {
1111
'use strict';
1212
13-
// Fallback: show page after max timeout no matter what
14-
var MAX_WAIT = 3000;
1513
var revealed = false;
1614
1715
var reveal = function() {
1816
if (revealed) return;
1917
revealed = true;
20-
document.documentElement.style.visibility = '';
21-
document.documentElement.style.opacity = '';
18+
document.documentElement.classList.remove('fonts-loading');
2219
};
2320
24-
// Safety timeout - never block page forever
25-
setTimeout(reveal, MAX_WAIT);
21+
setTimeout(reveal, 3000);
2622
27-
// If FontFace API not supported, just wait a bit and show
2823
if (!('FontFace' in window) || !('fonts' in document)) {
29-
setTimeout(reveal, 500);
24+
setTimeout(reveal, 100);
3025
return;
3126
}
3227
33-
// Define ALL fonts we need to load
3428
var uiRoot = '{{{uiRootPath}}}';
3529
var fonts = [
3630
{
@@ -55,23 +49,25 @@
5549
}
5650
];
5751
58-
// Load all fonts via FontFace API
5952
var loadPromises = fonts.map(function(f) {
60-
var face = new FontFace(f.family, 'url("' + f.url + '")', f.descriptors);
61-
return face.load().then(function(loaded) {
62-
document.fonts.add(loaded);
63-
return loaded;
64-
});
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+
}
6564
});
6665
6766
Promise.all(loadPromises)
6867
.then(function() {
6968
return document.fonts.ready;
7069
})
7170
.then(reveal)
72-
.catch(function(err) {
73-
console.warn('Font loading error:', err);
74-
reveal();
75-
});
71+
.catch(reveal);
7672
})();
7773
</script>

0 commit comments

Comments
 (0)