npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fout-loader

v0.3.1

Published

A font loader that implements the Flash of Unstyled Text ("FOUT with a class") algorithm

Downloads

14

Readme

FOUT Loader - Flash of Unstyled Text with a class

A font loader that implements the Flash of Unstyled Text ("FOUT with a class") algorithm, as described by Zach Leat.

This library simplifies the process of styling and loading web fonts in such a way that the user notices minimal "flash" while your fonts are still loading. It works by loading fonts in the background using Font Face Observer and setting a custom class on a (configurable) DOM element once each font loads. On subsequent loads of your page, FOUT Loader checks localStorage for a key unique to each web font as a heuristic for whether the font has been cached, in which case by definition we don't need to load the font again.

The result is a much smoother experience for the end user and a practically instantaneous rendering of web fonts on subsequent page loads.

Installation

npm install fout-loader --save

Usage

import fout from 'fout-loader'

fout({
  fontName: 'Merriweather', // required; works for any font supported by Font Face Observer
  fontLoadedClass: 'merriweather-loaded', // required
  container: document.querySelector('.my-container'), // defaults to document.documentElement
  localStorageKey: 'merriweather-loaded', // defaults to `fout-loader__${options.fontName}`
})

On-font-loaded callback

The fout() function returns the Promise returned by Font Face Observer, so if you want to do anything extra once your font has loaded, you can do so in a then() callback:

fout({ /* ... */ }).then(() => {
  console.log('did some other stuff')
})

Loading multiple fonts

Just like with Font Face Observer, you can simply call it multiple times. It's just a function, so you don't need to instantiate anything:

fout({
  fontName: 'Lato',
  fontLoadedClass: 'Lato',
})
fout({
  fontName: 'Lora',
  fontLoadedClass: 'Lora',
})

Avoiding shifting text

To avoid jarring shifts in the rendered text as the font changes, I highly recommend you check out the awesome Font Style Matcher by Monica Dinculescu. Using this tool, you can hone in a good fallback style to apply to a system font while your web font loads. It only takes a couple minutes of fiddling, and it's actually pretty fun to play with!

Once you arrive at a good combination of fallback/web font styles, apply them in your CSS by nesting the desired styles under the fontLoadedClass selector. For example, if your fontLoadedClass is "merriweather-loaded", use this CSS:

.copy-text-selector {
  font-family: Georgia;
  font-size: 16px;
  line-height: 1.6;
  letter-spacing: -0.35px;
  word-spacing: -0.35px;
}

.merriweather-loaded .copy-text-selector {
  font-family: Merriweather;
  line-height: 1.6;
  letter-spacing: 0;
  word-spacing: 0;
}

Caveats

  • Even if fonts are cached, instantaneous web font renders rely on localStorage being available. If localStorage is not available or the key for a given font has been deleted for any reason, FOUT Loader will fall back to loading the font over the network, resulting in a FOUT. This is the case even if the font is actually cached: the key word here is heuristic, the technical term for "pretty good indicator."
  • Conversely, if the user has cleared their cache but not localStorage, the class will be added prematurely and a flash will occur as FOUT Loader downloads it. This won't occur under normal circumstances.