|
| 1 | +# Gatsby Omni Font Loader |
| 2 | + |
| 3 | +Performant asynchronous font loading plugin for Gatsby. |
| 4 | + |
| 5 | +:globe_with_meridians: Supports web fonts |
| 6 | +<br/> |
| 7 | +:house: Suports self-hosted fonts |
| 8 | +<br/> |
| 9 | +:trident: Loads fonts asynchronously to avoid render blocking |
| 10 | +<br/> |
| 11 | +:stopwatch: Implemented with [fast loading snippets](https://csswizardry.com/2020/05/the-fastest-google-fonts/) |
| 12 | +<br/> |
| 13 | +:eyes: Loading status watcher for avoiding FOUT & FOUC |
| 14 | + |
| 15 | +## Configuration |
| 16 | + |
| 17 | +Add the following snippet to `gatsby-config.js` plugins array. |
| 18 | + |
| 19 | +```js |
| 20 | +{ |
| 21 | + /* Include plugin */ |
| 22 | + resolve: "gatsby-omni-font-loader", |
| 23 | + |
| 24 | + /* Plugin options */ |
| 25 | + options: { |
| 26 | + |
| 27 | + /* Enable font loading listener to handle FOUC */ |
| 28 | + enableListener: true, |
| 29 | + |
| 30 | + /* Preconnect URL-s. This example is for Google Fonts */ |
| 31 | + preconnect: ["https://fonts.gstatic.com"], |
| 32 | + |
| 33 | + /* Self-hosted fonts config. Add font files and font CSS files to "static" folder */ |
| 34 | + custom: [ |
| 35 | + { |
| 36 | + name: ["Font Awesome 5 Brands", "Font Awesome 5 Free"], |
| 37 | + file: "/fonts/fontAwesome/css/all.min.css", |
| 38 | + }, |
| 39 | + ], |
| 40 | + |
| 41 | + /* Web fonts. File link should point to font CSS file. */ |
| 42 | + web: [{ |
| 43 | + name: "Staatliches", |
| 44 | + file: "https://fonts.googleapis.com/css2?family=Staatliches", |
| 45 | + }, |
| 46 | + ], |
| 47 | + }, |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## Handling FOUC with Font loading watcher |
| 52 | + |
| 53 | +When loading fonts asynchronously, Flash Of Unstyled Content (FOUC) might happen because fonts load few moments later after page is displayed to the user. |
| 54 | + |
| 55 | +To avoid this, we can use CSS to style the fallback font to closely match the font size, line height and letter spacing of the main font that is being loaded. |
| 56 | + |
| 57 | +When `enableListener: true` is set in plugin config in `gatsby-config.js`, HTML classes are being added to `<body>` element as the fonts are being loaded. |
| 58 | + |
| 59 | +HTML class name format will be in the following format `wf-[font-family-name]--loaded`. |
| 60 | + |
| 61 | +You can use the [Font Style Matcher](https://meowni.ca/font-style-matcher/) to adjust the perfect fallback font and fallback CSS config. |
| 62 | + |
| 63 | +Here is the example of how `body` element will look like after all fonts are being loaded (depending on the config). |
| 64 | + |
| 65 | +```html |
| 66 | +<body class="wf-lazy-monday--loaded wf-font-awesome-5-brands--loaded wf-font-awesome-5-free--loaded wf-staatliches--loaded wf-henny-penny--loaded"> |
| 67 | +``` |
| 68 | + |
| 69 | +<img alt="FOUC example" src="https://res.cloudinary.com/dazdt97d3/image/upload/v1604140006/fouc.gif"> |
| 70 | + |
| 71 | + |
| 72 | +## Issues and Contributions |
| 73 | + |
| 74 | +Feel free to report and issues in the "Issues tab" and feel free to contribute to the project by creating Pull Requests. |
| 75 | + |
| 76 | +Contributions are welcome and appreciated! |
| 77 | + |
| 78 | +## Support |
| 79 | + |
| 80 | +The project is created and maintained by [Adrian Bece](https://codeadrian.github.io/) with the generous help of community contributors. If you have used the plugin and would like to contribute, feel free to [Buy Me A Coffee](https://www.buymeacoffee.com/ubnZ8GgDJ). |
| 81 | + |
| 82 | +<a href="https://www.buymeacoffee.com/ubnZ8GgDJ" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-red.png" alt="Buy Me A Coffee" style="height: 51px !important;width: 217px !important;" ></a> |
0 commit comments