Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fonts best practice for loading fonts #10475

Closed
ahansson89 opened this issue Dec 14, 2018 · 3 comments · May be fixed by ajesse11x/gatsby#424, saurabharch/gatsby#379, MaxMood96/gatsby#30 or saurabharch/gatsby#2051
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@ahansson89
Copy link
Contributor

What is the best way to load fonts in Gatsby? I currently just import the typeface into my component like this:

import "typeface-pacifico"
import "typeface-raleway"
import "typeface-open-sans"

I wonder if there is a better way, as i get Minimize Critical Requests Depth in Lighthouse.

@LekoArts LekoArts added the type: question or discussion Issue discussing or asking a question about Gatsby label Dec 14, 2018
@LekoArts
Copy link
Contributor

LekoArts commented Dec 14, 2018

Hi! 👋
Using self-hosted fonts is better than Google fonts, so that's a good start.

You should import the fonts in your Layout component (the one that wraps all pages). Other than that you don't have to do anything else. You could also preload them (as seen here) but I personally don't see the need for that.

Slightly unrelated note:
It's pretty common to use maximum two typefaces (from a design standpoint) so maybe you can even get rid of one.

@ahansson89
Copy link
Contributor Author

ahansson89 commented Dec 17, 2018

Thanks @LekoArts!

This is how I ended doing it in a dynamic way in gatsby-ssr.js. If anyone wants to turn it into a gatsby plugin, feel free.

const React = require('react')
const fs = require('fs')

exports.onRenderBody = ({ setHeadComponents }) => {

  const files = getFilesFromPath("./public/static", ".woff2")
  const preload = [
      'raleway-latin-400',
      'raleway-latin-700',
      'rochester-latin-400',
      'source-sans-pro-latin-400',
      'source-sans-pro-latin-700'
    ]

  setHeadComponents([
    files.map((file, i) => {
      return preload.map((font, key) => {
        const fileBeginning = file.split('-').slice(0,-1).join('-')
        if (fileBeginning === font) {
          return(
            <link
      				key={key}
      				rel='preload'
      				as='font'
      				type='font/woff2'
      				crossOrigin='anonymous'
      				href={`/static/${file}`}
      			/>
          )
        }
      })
    })
  ])
}

function getFilesFromPath(path, extension) {
  let dir = fs.readdirSync( path );
  return dir.filter( elm => elm.match(new RegExp(`.*\.(${extension})`, 'ig')));
}

@KyleAMathews
Copy link
Contributor

You might be interested in my proposal here: #14281

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment