Skip to content

Commit 889d124

Browse files
update the docs for fonts
1 parent 2ae5b2b commit 889d124

File tree

2 files changed

+21
-137
lines changed

2 files changed

+21
-137
lines changed

app/styles/tailwind.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@custom-variant dark (&:is(.dark *));
55

66
.font-title {
7-
font-family: "Panchang", sans-serif;
7+
font-family: "Poppins", sans-serif;
88
}
99

1010
:root {

docs/fonts.md

Lines changed: 20 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -6,147 +6,31 @@ easy to do so.
66

77
## Using Custom Fonts
88

9-
You can use custom fonts by creating the `./public/fonts` directory (if it
10-
doesn't already exist) and adding them to it.
11-
[Google Fonts](https://fonts.google.com/) is a good place to find open source
12-
fonts. You will also need to add the `css` file for the font to the
13-
`./app/styles` directory, if your font doesn't come with one (Google Fonts
14-
don't) you can generate one using a tool like
15-
[Transfonter](https://transfonter.org/). Transfonter now has a fonts directory
16-
setting. Set that to `fonts` to have the `url` preset.
9+
For custom fonts, Epic Stack uses [`fontless`](https://github.com/unjs/fontaine/tree/main/packages/fontless) for automatic font optimization. This provides zero-runtime font loading with proper fallbacks to reduce Cumulative Layout Shift (CLS).
1710

18-
Verify the `url` in the `css` is relative to the `public` folder. So it should
19-
look something like `url('/fonts/yourfont/yourfont-200.woff2')`.
11+
## How it works
2012

21-
Now you've added your font, there's a few places you need to update to use it.
13+
Simply use font families in your CSS and `fontless` handles the rest:
2214

23-
1. Add your font to the CSS variables.
15+
```css
16+
.font-title {
17+
font-family: "Poppins", sans-serif;
18+
}
2419

25-
```css
26-
/* tailwind.css */
27-
@layer base {
28-
:root {
29-
--font-sans: <YourFont>;
30-
}
31-
}
32-
```
20+
.body-text {
21+
font-family: "Inter", sans-serif;
22+
}
23+
```
3324

34-
2. Import the default theme and add your font to the `fontFamily` property.
25+
The plugin will:
26+
- Detect font-family declarations in your CSS
27+
- Resolve fonts from providers (Google Fonts, Bunny Fonts, etc.)
28+
- Generate optimized `@font-face` declarations
29+
- Add metric-based fallback fonts to reduce CLS
30+
- Download and serve font assets from `/_fonts/`
3531

36-
```ts
37-
import defaultTheme from 'tailwindcss/defaultTheme.js'
38-
// tailwind.config.ts
39-
extend: {
40-
...extendedTheme,
41-
fontFamily: {
42-
sans: ['var(--font-sans)', ...defaultTheme.fontFamily.sans],
43-
}
44-
}
32+
## Troubleshooting
4533

46-
```
34+
### Fonts not loading in development
35+
This is expected behavior. Fonts are only generated in the build files. To see fonts in development mode, build the application first and then run `npm run dev`.
4736

48-
3. Import your font stylesheet.
49-
50-
```tsx
51-
// app/routes/root.tsx
52-
import fontStyleSheetUrl from './styles/yourfont.css?url'
53-
```
54-
55-
Add the font stylesheet to the links array.
56-
57-
```tsx
58-
// app/routes/root.tsx
59-
...
60-
{ rel: 'preload', href: fontStyleSheetUrl, as: 'style' },
61-
{ rel: 'stylesheet', href: fontStyleSheetUrl },
62-
```
63-
64-
4. Expose and cache your fonts folder.
65-
66-
```ts
67-
// server/index.ts
68-
...
69-
app.use(
70-
'/fonts',
71-
// Can aggressively cache fonts as they don't change often
72-
express.static('public/fonts', { immutable: true, maxAge: '1y' }),
73-
)
74-
```
75-
76-
That's it! You can now use your custom font should now be available to use in
77-
your site.
78-
79-
## Font Metric Overrides
80-
81-
When using custom fonts, your site elements may stretch or shrink to accommodate
82-
the font. This is because the browser doesn't know the dimensions of the font
83-
you're using until it arrives, which introduces Cumulative Layout Shift and
84-
impact its web vitals.
85-
86-
In Epic Stack, we fixed this by introducing
87-
[Font Metric Overrides](https://github.com/epicweb-dev/epic-stack/pull/128/files).
88-
89-
Follow the steps below to add Font Metric Overrides to your custom fonts.
90-
91-
1. Generate the overrides for your font.
92-
93-
You can use [fontpie](https://www.npmjs.com/package/fontpie) utility to
94-
generate the overrides. For each of your fonts, write the following in your
95-
terminal:
96-
97-
```bash
98-
npx fontpie ./local/font/location.woff2 -w font-weight -s normal/italic -n YourFont
99-
```
100-
101-
#### Example
102-
103-
```sh
104-
npx fontpie ./public/fonts/nunito-sans/nunito-sans-v12-latin_latin-ext-200.woff2 -w 200 -s normal -n NunitoSans
105-
```
106-
107-
```css
108-
@font-face {
109-
font-family: 'NunitoSans Fallback';
110-
font-style: normal;
111-
font-weight: 200;
112-
src: local('Arial');
113-
ascent-override: 103.02%;
114-
descent-override: 35.97%;
115-
line-gap-override: 0%;
116-
size-adjust: 98.13%;
117-
}
118-
```
119-
120-
If you've got a lot of font files to override, you can use
121-
[fontpie-from-css](https://github.com/matt-kinton/fontpie-from-css) to
122-
generate the overrides from a CSS file.
123-
124-
```sh
125-
npx fontpie-from-css ./public/fonts/yourfont/yourfont.css
126-
```
127-
128-
**_Note:_** _If you've been following the steps above, you might have to copy
129-
your `yourfont.css` file temporarily to the `./public` folder as
130-
`fontpie-from-css` loads fonts relative to the CSS file._
131-
132-
2. Add the overrides to your font stylesheet.
133-
134-
Use fontpie for every custom font used (including variants) or
135-
fontpie-from-css and add the metric overrides to `yourfont.css`.
136-
137-
_Ensure the original font has the `font-display: swap` property or the
138-
fallback wouldn't work!_
139-
140-
3. Add the font fallback to the stylesheet.
141-
142-
```css
143-
/* tailwind.css */
144-
@layer base {
145-
:root {
146-
--font-sans: <YourFont> <YourFontFallback>;
147-
}
148-
}
149-
```
150-
151-
That's it! You can now use your custom font without worrying about Cumulative
152-
Layout Shift!

0 commit comments

Comments
 (0)