Skip to content

chrisnajman/self-hosting-fonts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

permalink
/index.html

Website (Git Pages)

Self-hosting fonts

Many of us use Google fonts via a CDN. However, Germany has decided that this contravenes GDPR, leaving site owners open to litigation.

We can still display Google fonts by:

  1. Downloading the font (variants) from Google Fonts, then
  2. converting the .ttf files to .woff and .woff2 using a web font generator, then
  3. using @font-face in the CSS and
  4. preloading the fonts in the HTML.

Note: This page concentrates on Google Fonts, but the same principles apply to any other kind of web font.

Web font generators

Font Squirrel This worked for "Roboto" but failed with the static font versions of both "Lato" and "Open Sans".

Creative Fabrica I used this for "Lato" and "Open Sans" static files. I had to upload each variant, one at a time.

HTML

I was advised to preload the fonts in the head of the html page. This is supposed to speed up the delivery of the fonts and increase Google's Core Web Vitals (CWVs) performance. However, I saw warnings after doing this (see Testing/Firefox and Firefox Developer Edition, below).

<head>
  <link
    rel="preload"
    href="roboto-light-webfont.woff2"
    as="font"
    type="font/woff2"
    crossorigin="anonymous"
  />
  <link
    rel="preload"
    href="roboto-regular-webfont.woff2"
    as="font"
    type="font/woff2"
    crossorigin="anonymous"
  />
  <link
    rel="preload"
    href="roboto-medium-webfont.woff2"
    as="font"
    type="font/woff2"
    crossorigin="anonymous"
  />
  ...
  <!-- CSS file comes after preload -->
</head>

CSS

@font-face {
  font-family: "Roboto";
  font-weight: 300;
  src: url("roboto-light-webfont.woff2") format("woff2"), url("roboto-light-webfont.woff")
      format("woff");
}
/** Regular: 400 **/
@font-face {
  font-family: "Roboto";
  font-weight: 400;
  src: url("roboto-regular-webfont.woff2") format("woff2"), url("roboto-regular-webfont.woff")
      format("woff");
}
/** Medium: 500 **/
@font-face {
  font-family: "Roboto";
  font-weight: 500;
  src: url("roboto-medium-webfont.woff2") format("woff2"), url("roboto-medium-webfont.woff")
      format("woff");
}

Testing

Acknowledgements

The bulk of the information came from Self-hosting fonts explained (including Google fonts) // @font-face tutorial (YouTube). Information on preloading/CWVs, and non-GDPR compliance emerged from a comments thread on the same page.

Releases

No releases published

Packages

No packages published