Skip to content

Commit caa8725

Browse files
author
Adrian Bece
committed
Fix docs, improve utils
1 parent f89f382 commit caa8725

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
Performant asynchronous font loading plugin for Gatsby.
44

5-
:globe_with_meridians: Supports web fonts
6-
<br/><br/>
7-
:house: Suports self-hosted fonts
8-
<br/><br/>
9-
:trident: Loads fonts asynchronously to avoid render blocking
10-
<br/><br/>
11-
:stopwatch: Implemented with [fast loading snippets](https://csswizardry.com/2020/05/the-fastest-google-fonts/)
12-
<br/><br/>
13-
:eyes: Loading status watcher for avoiding FOUT & FOUC
5+
* Supports web fonts & self-hosted fonts
6+
* Suports self-hosted fonts
7+
* Preloads the files & preconnects to the URL
8+
* Loads fonts asynchronously to avoid render blocking
9+
* Implemented with [fast loading snippets](https://csswizardry.com/2020/05/the-fastest-google-fonts/)
10+
* Loading status listener for avoiding FOUT & FOUC
1411

1512
## Install
1613

@@ -41,22 +38,26 @@ Add the following snippet to `gatsby-config.js` plugins array.
4138
/* Self-hosted fonts config. Add font files and font CSS files to "static" folder */
4239
custom: [
4340
{
41+
/* Exact name of the font as defied in @font-face CSS rule */
4442
name: ["Font Awesome 5 Brands", "Font Awesome 5 Free"],
43+
/* Path to the font CSS file inside the "static" folder with @font-face definition */
4544
file: "/fonts/fontAwesome/css/all.min.css",
4645
},
4746
],
4847

4948
/* Web fonts. File link should point to font CSS file. */
5049
web: [{
50+
/* Exact name of the font as defied in @font-face CSS rule */
5151
name: "Staatliches",
52+
/* URL to the font CSS file with @font-face definition */
5253
file: "https://fonts.googleapis.com/css2?family=Staatliches",
5354
},
5455
],
5556
},
5657
}
5758
```
5859

59-
## Handling FOUC with Font loading watcher
60+
## Handling FOUC with Font loading listener
6061

6162
When loading fonts asynchronously, Flash Of Unstyled Content (FOUC) might happen because fonts load few moments later after page is displayed to the user.
6263

@@ -79,7 +80,7 @@ Here is the example of how `body` element will look like after all fonts are bei
7980

8081
## Issues and Contributions
8182

82-
Feel free to report and issues in the "Issues tab" and feel free to contribute to the project by creating Pull Requests.
83+
Feel free to [report issues](https://github.com/codeAdrian/gatsby-omni-font-loader/issues) you find and feel free to contribute to the project by creating Pull Requests.
8384

8485
Contributions are welcome and appreciated!
8586

gatsby-browser.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import { AsyncFonts, FontListener } from "./components"
21
import React from "react"
2+
import { AsyncFonts, FontListener } from "./components"
3+
import { getFontFiles, getFontNames } from "./utils"
34

45
export const wrapRootElement = (
56
{ element },
67
{ custom, web, enableListener }
78
) => {
89
const allFonts = [...custom, ...web]
9-
const fontNames = []
10-
const fontFiles = allFonts.map(({ file }) => file)
11-
allFonts.forEach(({ name }) =>
12-
Array.isArray(name) ? fontNames.push(...name) : fontNames.push(name)
13-
)
10+
const fontFiles = getFontFiles(allFonts)
11+
const fontNames = getFontNames(allFonts)
1412

1513
const hasFontFiles = Boolean(fontFiles.length)
1614
const hasFontNames = Boolean(fontNames.length)

gatsby-ssr.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { getFontConfig, getTestFonts } from "./utils"
1+
import {
2+
getFontConfig,
3+
getFontFiles,
4+
getFontNames,
5+
getTestFonts,
6+
} from "./utils"
27

38
export const onRenderBody = (
49
{ setHeadComponents, setPostBodyComponents },
510
{ enableListener, preconnect, web, custom }
611
) => {
712
const allFonts = [...web, ...custom]
8-
const preload = allFonts.map(({ file }) => file)
9-
const fontNames = []
10-
allFonts.forEach(({ name }) =>
11-
Array.isArray(name) ? fontNames.push(...name) : fontNames.push(name)
12-
)
13+
const preload = getFontFiles(allFonts)
14+
const fontNames = getFontNames(allFonts)
1315

1416
const hasPreconnect = Boolean(preconnect.length)
1517
const hasPreload = Boolean(preload.length)

utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./getFontConfig"
22
export * from "./getTestFonts"
3+
export * from "./utils"

utils/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const getFontNames = (allFonts: { name: string }[]) => {
2+
const fontNames = []
3+
allFonts.forEach(({ name }) =>
4+
Array.isArray(name) ? fontNames.push(...name) : fontNames.push(name)
5+
)
6+
return fontNames
7+
}
8+
9+
export const getFontFiles = (allFonts: { file: string }[]) =>
10+
allFonts.map(({ file }) => file)

0 commit comments

Comments
 (0)