Repository navigation
Experimental SvelteKit fonts (@sveltejs/enhanced-fonts) #17569
Replies: 1 comment
|
Added an opt-in ESLint export at @sveltejs/enhanced-fonts/eslint (not a separate package). It uses svelte-eslint-parser so it runs in the editor, not at compile time. Three rules, all errors in the recommended flat config for .svelte and app.html: no-google-fonts-cdn — flags / @import to fonts.googleapis.com and fonts.gstatic.com. Leaves self-hosted files and other CDNs alone. Still on the fork (experimental-fonts). Playground demos are up for a few more weeks while the host tokens last. |
Uh oh!
There was an error while loading. Please reload this page.
Svelte and SvelteKit are already strong on performance. That’s why font products have been a hard sell: anything that delays LCP or pretends preload is free fails the same bar the rest of the stack is held to.
DIY isn’t great — Performance still leaves fonts as a manual job (kit#1619).
Preloading everything isn’t great either (kit#10596).
This will not be Svelte or Kit core: an experimental
@sveltejs/enhanced-fontspackage, the same kind of split as@sveltejs/enhanced-img.Proposal
An opt-in, experimental Vite plugin,
@sveltejs/enhanced-fonts: declarefamilies[]invite.config.js, inject@font-facewith<Font>, apply in CSS with--font-*. Defaultpreload: false. Hashed.woff2lands in the client output beforeadapt(), so existingwriteClient()copies them. Plugin aftersveltekit(). A Kitexperimental.fontsflag only if we later generate$app/fonts.Unknown
cssVariable, a name that isn’t--font-*, or a family that doesn’t resolve should fail the build (warn in dev). Two<Font>s for the same variable on one document should fail.Under the hood, unifont downloads the files; app code never imports it.
Preload stays off unless the document asks
Preload is an HTML
<link rel="preload" as="font">from<Font preload>on that document — shared font A on/,/b,/cis preloaded only on/; after that, HTTP cache serves A. Putting every font preload in root+layout/app.htmlis the #10596 Lighthouse hole.handle+resolve({ preload })is plumbing, not discovery: walking the CSS graph (or a global list) repeats #7704/#10596. HTML<link>still works on static/Pages wherehandlenever runs; laterLinkheaders can list the same opted-in files.//b/c@font-face/ CSS for A<link rel=preload as=font>A/only/conlyAssert HTML (and optional
Linkheaders), not “network downloaded.”Warnings
Leftover Google CDN
<link>/@import: ESLint. Same URLs in the Vite CSS graph: package warning. UnknowncssVariable/ failed resolve: throw. Do not put CDN policy in the Svelte compiler.Unused family: not yet. Warn by the time this is stable v1.
What isnt't being done in this experimental cut
font-familyscan as the API (this cut is a config list)fontless/runtimeJS in+layout.sveltetransformIndexHtml(Kituses_unsupported_html_hook)adapter.fonts/builder.writeFontsstatic/font-display: optionalsv addof community fontless — this package is@sveltejs/enhanced-fontsoptimizeFontsruntime CSS inlining (vercel/next.js#69137)Possible roadmap to v1
className/variable, or generated$app/fontsfont())import { Inter } from 'next/font/google')<Font>detector if the document-level error needs a route-graph check (layout + page)detect: truewith warnings on unresolved families (unlike Nuxt silence)subset-font/ harfbuzz peerLinkheaders /FontDependencyweight+subset if auto-preload-by-filename needs itgetFontFileURLwith a name that can stay (not a leftoverexperimental_prefix)Demos
One playground, six adapters. Pages
baseis/sveltekit-fonts-playground. Render free tier cold-starts. Vercel/Netlify are public (no SSO). Fontsource +resolve({ preload })stay valid; this is additive.adapter-static(GitHub Pages)adapter-bun(Render bun)adapter-node(Render node)adapter-verceladapter-cloudflareadapter-netlifyReproductions
Not an upstream Kit PR — a public reproduction of the experimental cut:
experimental-fonts): https://github.com/fringe4life/kitOther frameworks
Next
display: swap; CJK download without preload; ESLint on leftover Google<link>preload: trueplus root-layout fonts still produces unused-preload; named Google exports (import { Inter } from 'next/font/google') are a possible v1 DX, not this experimental cut; dummdidumm on kit#4963: Next’s image component loads on demand — preloading everything would hurt CWV; official examples don’t split “CSS on every route / preload once” — Next needs a second loader instance for thatpreload: trueNuxt
--font-*; fail closed in productionfont-familyscan as the API; treatingsv addas a Nuxt-style module host (scaffold-once only)Astro
families[](what this experimental cut borrows);<Font>;var(--font-*); hashed files onto client outDir; no fonts adapter hook; component-level preload default offQuestions
9. Prerender OG fonts. Astro starts an ephemeral HTTP server because
dist/is not readable yet. Kit analog:file:URLs, an ephemeral server, or skip until later?10. Skew-protection query params. New optional adapter field (Astro Vercel/Netlify
?dpl=), or wait until a host actually needs it?11.
FontDependency. Is{ file, filename }(kit#16443) enough, or do we need weight/subset on the manifest before this is stable?All reactions