Self-host Google Fonts in your next React project with Fontsource
Tags: javascript, react, webdev, fonts
Published: 2020-12-12 12:25:02

## The situation 🤔

While you can rely on Google's CDN to host and serve your fonts so you don't have to, you're relying on that connection and the fonts themselves being available. That's fine for a quick mock-up, but not so great for your production roll-out.

## Typefaces—the status quo 👌

The [Typefaces project](https://github.com/KyleAMathews/typefaces) has been the long-standing approach to self-hosting Google fonts with your React web apps, but it wasn't without its caveats, as explained in the opening lines of the project's [README.md](https://github.com/KyleAMathews/typefaces/blob/master/README.md):

> The Typefaces project is now deprecated.
> 
> @DecliningLotus created FontSource which provides the same functionality as Typefaces but with automated releases & richer support for importing specific weights, styles, or language subsets.

## Enter, Fontsource 🙇‍♂️

With [Fontsource](https://fontsource.org), you get a number of great benefits right out of the gate:

* Ridiculous performance gains from self-hosting ([source](https://github.com/HTTPArchive/almanac.httparchive.org/pull/607), [source](https://github.com/reactiflux/reactiflux.com/pull/21)).
* Version locking ensures your fonts are free from Google's tinkering.
* Your fonts will be available even if your React app is running offline.
* Fontsource also includes Open Source fonts, not just Google's library.

Why are we still talking about it? Let's use some fonts already!

## Using Fontsource with React 🔤

As an example, let's use the Google Font "[Poppins](https://fonts.google.com/specimen/Poppins)".

A note from the installation docs:

> Fontsource assumes you are using a bundler, such as Webpack, to load in CSS. Solutions like CRA, Gatsby and Next.js are prebuilt examples that are compatible.

Now, on to the fun part:

1. In your project directory, install the font:```bash
npm install @fontsource/poppins
```
1. In your app or index JS file, import the font:```javascript
import "@fontsource/poppins" // Defaults to weight 400 with all styles included.
```
1. Alternatively, in your project SCSS file, prepend the following to the top:```scss
@import "~@fontsource/poppins/index.css"; // Defaults to weight 400 with all styles included.
```
1. Lastly, use the font in your CSS/SCSS rules:```css
body {
  font-family: "Poppins";
}
```

And that's it!

As long as your chosen font supports it, you can optionally import other subsets that you need, for example:

```scss
@import "~@fontsource/poppins/700.css"; /* Font-weight 700 */
@import "~@fontsource/poppins/600-italic.css"; /* Font-weight 600 italic */
```

## But do they have x font? 🙄

Fontsource has an amazing [search directory](https://fontsource.org/fonts) that lists all supported fonts. It also allows you to interactively adjust font-size on the fly.

Better still, every font page lists the npm installation commands and JavaScript/SCSS import scripts needed to get you up and running in seconds.

---

You now have all the knowledge you need to make your React app beautiful with any self-hosted font you could want (aside from [comic-neue](https://fontsource.org/fonts/comic-neue) and [lobster](https://fontsource.org/fonts/lobster), because you want your React app to look beautiful, right? 😅).

---

Cover photo source: <span>Photo by <a href="https://unsplash.com/@marcusdepaula?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Marcus dePaula</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>

**Edit 30/08/2021:** Updated Fontsource package names (as they were moved from fontsource-[font] to @fontsource/[font].
**Edit 30/08/2021:** Updated Fontsource links (thank you, @jerhowden! 🙇‍♂️)