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:
Gigi
2025-10-06 20:04:11 +01:00
parent d9db10fd70
commit b01293aa20
3 changed files with 97 additions and 21 deletions

View File

@@ -39,13 +39,15 @@ const Settings: React.FC<SettingsProps> = ({ settings, onSave, onClose }) => {
useEffect(() => {
// Preload all fonts for the dropdown
const fonts = ['inter', 'lora', 'merriweather', 'open-sans', 'roboto', 'source-serif-4', 'crimson-text', 'libre-baskerville', 'pt-serif']
fonts.forEach(font => loadFont(font))
fonts.forEach(font => {
loadFont(font).catch(err => console.warn('Failed to preload font:', font, err))
})
}, [])
useEffect(() => {
// Load font for preview when it changes
const fontToLoad = localSettings.readingFont || 'source-serif-4'
loadFont(fontToLoad)
loadFont(fontToLoad).catch(err => console.warn('Failed to load preview font:', fontToLoad, err))
}, [localSettings.readingFont])
// Auto-save settings whenever they change (except on initial mount)