-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(website): add preload guide (#963)
* docs: add preload guide * perf(website): preload site fonts * docs: formatting and style * docs: add url link to open sans install page
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: Font Display | ||
section: Contents | ||
--- | ||
|
||
## Font Display | ||
|
||
--- | ||
|
||
Font display is a CSS property that controls how fonts appear on your website. By default, Fontsource uses the `swap` value for the `font-display` property. This means that while a custom font is loading, the browser displays a fallback font. Once the custom font is ready, the browser swaps it in place of the fallback font. | ||
|
||
> **Note:** You can learn more about the `font-display` property in the [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display). | ||
To customise the `font-display` property for a specific font, you can use the **Advanced** tab on each **[Install](https://fontsource.org/fonts/open-sans/install)** page to generate custom `@font-face` rules with your preferred value. | ||
|
||
<br /> | ||
|
||
For example, to set the `font-display` property to `optional` for Open Sans. You can create the following `@font-face` rule: | ||
|
||
```css | ||
@font-face { | ||
font-family: 'Open Sans Variable'; | ||
font-style: normal; | ||
font-display: optional; | ||
font-weight: 300 800; | ||
src: url(@fontsource-variable/open-sans/files/open-sans-latin-wght-normal.woff2) format('woff2-variations'); | ||
unicode-range: U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+2074,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD; | ||
} | ||
``` | ||
|
||
> **Note:** This method is only compatible with **Vite-based** frameworks or similar setups that support CSS bundling with `url()` rewrites. | ||
You can then include this rule in your global CSS file and apply it to your components using the following CSS: | ||
|
||
```css | ||
body { | ||
font-family: 'Open Sans Variable', sans-serif; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: Preloading Fonts | ||
section: Contents | ||
--- | ||
|
||
## Preloading Fonts | ||
|
||
--- | ||
|
||
Preloading fonts is a technique used to load fonts before they're required, which can improve your website's performance. However, it's essential to preload only critical fonts and subsets to avoid unnecessarily increasing initial load times. | ||
|
||
> **Note:** You can learn more about the `preload` link relation type in the [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload). | ||
To preload a font, you can use the `preload` link relation type in the `<head>` of your HTML document. However, due to bundler constraints, you must manually import the URL to your font file. This is typically challenging since bundlers like Vite rewrite URLs to hashed filenames. | ||
|
||
<br /> | ||
|
||
Instead, you can use the Vite [static asset directive](https://vitejs.dev/guide/assets.html#explicit-url-imports) `?url` to import the font file URL. For instance, the following code highlights how to preload the Open Sans font: | ||
|
||
```jsx | ||
import '@fontsource-variable/open-sans'; | ||
import openSansWoff2 from '@fontsource-variable/open-sans/files/open-sans-latin-wght-normal.woff2?url'; | ||
|
||
const App = () => { | ||
return ( | ||
<html lang="en"> | ||
<head> | ||
<link rel="preload" as="font" type="font/woff2" href={openSansWoff2} crossorigin/> | ||
</head> | ||
</html> | ||
); | ||
}; | ||
``` | ||
|
||
> **Note:** Avoid preloading all font files in your project, as this may lead to increased initial load times. Only preload critical fonts and subsets that are essential for the initial page display, such as the latin subset only. | ||
> <br /> | ||
> Additionally, preloading can have negative effects on Core Web Vitals if not used correctly. See the [Web Vitals](https://web.dev/articles/preload-critical-assets#effects_of_preloading_on_core_web_vitals) article for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters