mirror of
https://github.com/dergigi/boris.git
synced 2026-01-11 02:44:42 +01:00
fix: ensure fonts are fully loaded before applying styles
- Convert loadFont to async function that returns a Promise - Use Font Loading API to wait for fonts to be actually ready - Add comprehensive logging for font loading stages - Wait for font loading in useSettings before applying CSS variables - Update Settings component to handle async font loading - Prevents FOUT (Flash of Unstyled Text) by ensuring fonts are ready - Fixes timing issue where custom fonts weren't being applied consistently This ensures custom fonts are fully loaded and ready before being applied, eliminating the race condition where content would render with system fonts before custom fonts were available.
This commit is contained in:
@@ -12,25 +12,83 @@ const FONT_FAMILIES: Record<string, string> = {
|
||||
}
|
||||
|
||||
const loadedFonts = new Set<string>()
|
||||
const loadingFonts = new Map<string, Promise<void>>()
|
||||
|
||||
export function loadFont(fontKey: string) {
|
||||
if (fontKey === 'system' || loadedFonts.has(fontKey)) {
|
||||
return
|
||||
export async function loadFont(fontKey: string): Promise<void> {
|
||||
if (fontKey === 'system') {
|
||||
console.log('📝 Using system font')
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
if (loadedFonts.has(fontKey)) {
|
||||
console.log('✅ Font already loaded:', fontKey)
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
// If font is currently loading, return the existing promise
|
||||
if (loadingFonts.has(fontKey)) {
|
||||
console.log('⏳ Font already loading:', fontKey)
|
||||
return loadingFonts.get(fontKey)!
|
||||
}
|
||||
|
||||
const fontFamily = FONT_FAMILIES[fontKey]
|
||||
if (!fontFamily) {
|
||||
console.warn(`Unknown font: ${fontKey}`)
|
||||
return
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
// Create a link element to load the font from Bunny Fonts
|
||||
const link = document.createElement('link')
|
||||
link.rel = 'stylesheet'
|
||||
link.href = `https://fonts.bunny.net/css?family=${encodeURIComponent(fontFamily.toLowerCase().replace(/ /g, '-'))}:400,400i,700,700i`
|
||||
document.head.appendChild(link)
|
||||
console.log('🔤 Loading font:', fontFamily)
|
||||
|
||||
loadedFonts.add(fontKey)
|
||||
// Create a promise for this font loading
|
||||
const loadPromise = new Promise<void>((resolve) => {
|
||||
// Create a link element to load the font from Bunny Fonts
|
||||
const link = document.createElement('link')
|
||||
link.rel = 'stylesheet'
|
||||
link.href = `https://fonts.bunny.net/css?family=${encodeURIComponent(fontFamily.toLowerCase().replace(/ /g, '-'))}:400,400i,700,700i`
|
||||
|
||||
// Wait for the stylesheet to load
|
||||
link.onload = () => {
|
||||
console.log('📄 Stylesheet loaded for:', fontFamily)
|
||||
|
||||
// Use Font Loading API to wait for the actual font to be ready
|
||||
if ('fonts' in document) {
|
||||
Promise.all([
|
||||
document.fonts.load(`400 16px "${fontFamily}"`),
|
||||
document.fonts.load(`700 16px "${fontFamily}"`)
|
||||
]).then(() => {
|
||||
console.log('✅ Font ready:', fontFamily)
|
||||
loadedFonts.add(fontKey)
|
||||
loadingFonts.delete(fontKey)
|
||||
resolve()
|
||||
}).catch((err) => {
|
||||
console.warn('⚠️ Font loading failed:', fontFamily, err)
|
||||
loadedFonts.add(fontKey) // Mark as loaded anyway to prevent retries
|
||||
loadingFonts.delete(fontKey)
|
||||
resolve()
|
||||
})
|
||||
} else {
|
||||
// Fallback: just wait a bit for older browsers
|
||||
setTimeout(() => {
|
||||
console.log('✅ Font assumed ready (no Font Loading API):', fontFamily)
|
||||
loadedFonts.add(fontKey)
|
||||
loadingFonts.delete(fontKey)
|
||||
resolve()
|
||||
}, 100)
|
||||
}
|
||||
}
|
||||
|
||||
link.onerror = () => {
|
||||
console.error('❌ Failed to load font stylesheet:', fontFamily)
|
||||
loadedFonts.add(fontKey) // Mark as loaded to prevent retries
|
||||
loadingFonts.delete(fontKey)
|
||||
resolve() // Resolve anyway so we don't block
|
||||
}
|
||||
|
||||
document.head.appendChild(link)
|
||||
})
|
||||
|
||||
loadingFonts.set(fontKey, loadPromise)
|
||||
return loadPromise
|
||||
}
|
||||
|
||||
export function getFontFamily(fontKey: string | undefined): string {
|
||||
|
||||
Reference in New Issue
Block a user