typefaces icon indicating copy to clipboard operation
typefaces copied to clipboard

where to add require('fontname') in gatsby

Open lfeddern opened this issue 8 years ago • 8 comments

Hi there,

Atm using google fonts gastby package but would like to self host a google font to improve pagespeed.

Where in my gastby project should I add the require('fontname')?

Thanks v much.

lfeddern avatar Nov 17 '17 18:11 lfeddern

In your layout component.

KyleAMathews avatar Nov 21 '17 22:11 KyleAMathews

thanks!

lfeddern avatar Nov 24 '17 21:11 lfeddern

As an example (using ES6 Modules):

//layouts/index.js
import React from 'react';
import 'typeface-FONTNAME`; //eslint-disable-line

optional: suppress eslint missing css extension error

Then, reference in your CSS:

body {
  font-family: FONTNAME;
}

note: you may still experience a flash of un-styled text (FOUT)

skube avatar May 09 '18 15:05 skube

import 'typeface-roboto' seems to attempt to load a CSS file as Javascript source (I'm not using Gatsby, just Electron and Material-UI):

image

Is there another flag needed to make this work?

HebaruSan avatar May 21 '18 23:05 HebaruSan

@HebaruSan You need webpack or something similar to use typefaces from npm. Webpack uses loaders like css-loader to translate that require into whatever your environment is configured to do (inline css, extract to files, bundle, etc). How are you managing your assets?

Here's an example on how to setup webpack with material-ui.

DamianPereira avatar Jun 22 '18 21:06 DamianPereira

Thanks, but I think webpack was recommended against for Electron projects. Right now I am using electron-forge for packaging.

I eventually put this in my index.html instead, seems to work:

        <link rel="stylesheet" type="text/css" href="../node_modules/typeface-roboto/index.css" />

HebaruSan avatar Jun 22 '18 22:06 HebaruSan

Oh, didn't know webpack was not recommended for electron, it's what I used last time. Well linking the CSS works too, glad you could make it work.

DamianPereira avatar Jun 25 '18 05:06 DamianPereira

@KyleAMathews we are loading it in gatsby-browser.js. what is advantage/disadvantages in loading in gatsby-browser.js vs layout component.

nsisodiya avatar May 15 '19 13:05 nsisodiya