From bb8d2d685c9c7bc69d9a8cfcb015f018e572e483 Mon Sep 17 00:00:00 2001 From: Ilia Mashkov Date: Tue, 10 Feb 2026 13:08:07 +0300 Subject: [PATCH] feat(Layout): add font loading flag and change head links to prevent FOUT --- src/app/ui/Layout.svelte | 53 ++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/src/app/ui/Layout.svelte b/src/app/ui/Layout.svelte index d4a5835..a934407 100644 --- a/src/app/ui/Layout.svelte +++ b/src/app/ui/Layout.svelte @@ -15,32 +15,69 @@ import GD from '$shared/assets/GD.svg'; import { ResponsiveProvider } from '$shared/lib'; import { ScrollArea } from '$shared/shadcn/ui/scroll-area'; import { Provider as TooltipProvider } from '$shared/shadcn/ui/tooltip'; -import type { Snippet } from 'svelte'; +import { + type Snippet, + onMount, +} from 'svelte'; interface Props { children: Snippet; } -/** Slot content for route pages to render */ let { children }: Props = $props(); +let fontsReady = $state(false); + +/** + * Sets fontsReady flag to true when font for the page logo is loaded. + */ +onMount(async () => { + if (!('fonts' in document)) { + fontsReady = true; + return; + } + + const required = ['100']; + + const missing = required.filter( + w => !document.fonts.check(`${w} 1em Barlow`), + ); + + if (missing.length > 0) { + await Promise.all( + missing.map(w => document.fonts.load(`${w} 1em Barlow`)), + ); + } + fontsReady = true; +}); + +$inspect(fontsReady); + ((e.currentTarget as HTMLLinkElement).media = 'all'))} > - + Compare Typography & Typefaces | GlyphDiff @@ -55,7 +92,9 @@ let { children }: Props = $props();
- {@render children?.()} + {#if fontsReady} + {@render children?.()} + {/if}