2017 © Pedro Peláez
 

wordpress-plugin wp-font-fout

Optimise font loading, producing a FOUT instead of a FOIT

image

rareloop/wp-font-fout

Optimise font loading, producing a FOUT instead of a FOIT

  • Wednesday, December 16, 2015
  • by rareloop
  • Repository
  • 3 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Font FOUT

WordPress plugin that allows browsers to show a Flash Of Unstyled Text (FOUT) when using web fonts., (*1)

Setup

Install & activate the plugin, then tell the plugin what font families to listen for:, (*2)

<?php // functions.php

Rareloop\FontFout::init([
    'PT Serif',
    'Open Sans',
]);

This will add the required JavaScript inline to the bottom of your <body>. When all the fonts have loaded the fonts-loaded class will be added to your <html> element. You can use this in your CSS to control when your web font is used:, (*3)

body {
    font-family: 'Georgia, serif';
}

.fonts-loaded body {
    font-family: 'PT Serif, Georgia, serif';
}

More Configuration

The init function also take additional arguments that lets you control the cookie that is created to know when fonts are loaded. If you wanted to change the cookie name to _fonts_cached_in_browser and make it last 30 days you could do the following:, (*4)

<?php // functions.php

Rareloop\FontFout::init(['PT Serif'], '_fonts_cached_in_browser', 60 * 60 * 24 * 30);

Optimising for future page views

The JavaScript injected into your page will set a cookie once all fonts are loaded. Once loaded you can assume that your users browser will have cached the font so shouldn't need to download again. In this instance you can have the fonts-loaded class added to your <html> element before it's sent to the browser., (*5)

To add this optimisation you can use the following shortcode in your template:, (*6)

<!DOCTYPE html>
<html class="no-js [fontfout-htmlclass]">
<head>
    <title>...</title>
</head>
<body>
    ...
</body>
</html>

The shortcode will only add the class when the cookie is detected. It will also prevent the inline JavaScript from being injected, reducing the payload of future page loads., (*7)

The plugin adds the following cookie: fonts-loaded, which you should add to your Cookie/Privacy Policy., (*8)

The Versions

16/12 2015

dev-master

9999999-dev

Optimise font loading, producing a FOUT instead of a FOIT

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

16/12 2015

v1.0.0

1.0.0.0

Optimise font loading, producing a FOUT instead of a FOIT

  Sources   Download

The Requires

  • php >=5.5.0