Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Web Font Loader not cache the fonts?

Just wanted to test the ability of the Web Font Loader and surprisingly I have discovered that when I need to load the same font in another page then the loader performs new download instead of using a cached version of the font. Is this normal? If so, is there a simple way to check if the font is available for the browser, in other words whether it is cached?

Here's is how I load the font:

 <script>
    WebFont.load({
        custom: {
            families: ['Univers45custom', 'Univers45customIE']
        }
    });
</script>

I am using Web Font Loader v1.5.10.

Addendum by BramVanroy: this 'lack of caching' is also present when using Google's webfonts. FOUT (a Flash of Unstyled Text) briefly occurs on a website that uses the font loader even after reloading the page multiple times.

Edit by eldi: Hi BramVanroy -> Right now I am not really sure how I went around this issue, but probably I just used the @font-face. The reason why I tested the Web Font Loader was the FOUT in the first place. The Loader adds css class to html element which provide you a way to style your page without the right font, when the fonts are loaded then the class is gone and your "standard" styling is present. That was working as expected but with the 'lack of caching' exception, which in my situation was not acceptable. I believe that staypuftman workaround with modifying HTTP header would do the job, I do not have time to test it, especially I would need to do some research to find the way to set it in asp.net hosting provider as setting it from application will add additional processing time.

like image 508
eldi Avatar asked Jan 26 '15 13:01

eldi


People also ask

Can fonts be cached?

The operating system and certain applications use font caches to keep track of the fonts that are installed on you computer. A common issue when installing or updating fonts is that the font cache files on your computer can become corrupted.

Should I preload all fonts?

If you use multiple fonts, it might be tempting to preload every font that you use in every format. However, there are performance tradeoffs to using font preloading. With preload, you're essentially forcing browsers to make a high-priority request for the font URL whether or not it's needed.

What is Web Font Loader?

The Web Font Loader is a JavaScript library that gives you more control over font loading than the Google Fonts API provides. The Web Font Loader also lets you use multiple web font providers. It was co-developed by Google and Typekit.

Are Google fonts cached?

Google Fonts and Website PerformanceGoogle Fonts claims to make web pages load faster by caching fonts across various sites so that each font loads only once for all websites. The browser can use the same cached font for any other website that requests the same Google Font.


2 Answers

Web Font Loader doesn't have a caching provision but your browser is caching the font if - and only if - you are actually using it in your site somewhere. Check to make sure the font is invoked on the page in question somewhere.

You can make sure things are cached by forcing the HTTP cache control header (good run down of this at Google Developers). I usually set this through Apache, like so (there are many other ways to do this though):

#Set cache-control HTTP header for public with max time
Header set Cache-Control "max-age=290304000, public"

If all of that fails, the best way I can think of taking matters into your own hands would be to set a cookie, check for it and load fonts accordingly. In your situation, the code would look something like this:

// 1- Check for cookie, if it's not present we enter this conditional
if (!font_is_cached) {
  $(window).load(function() {

    // 2- Load your webfont, this would put the font in cache
    WebFont.load({
      custom: {
          families: ['Univers45custom', 'Univers45customIE']
      }
    });

    // 3- Set cookie indicating fonts are cached
    // This leverages Filament Group's cookie.js as a dependency
    // https://github.com/filamentgroup/cookie/blob/master/cookie.js
    // Sets a one-day session cookie
    cookie( "font_is_cached", "true", 1 );
  });
}

Additional Resources

  • Filament Group has a great font-loading rundown. They don't use web font loader but you could follow what they are doing.
  • CSS-tricks outlines something similar to what I have in mind but they are explicitly setting the cookie on the back-end before doing the front-end check/set function I laid out.
like image 138
serraosays Avatar answered Sep 25 '22 05:09

serraosays


WebFont Loader will cache the font into the web browser for the 1 year here is the detail how can you load webfont end detail home much time it will be in browser cache.

http://peterwilson.cc/caching-google-webfont-loader/

Web Font Loader

Web Font Loader gives you added control when using linked fonts via @font-face. It provides a common interface to loading fonts regardless of the source, then adds a standard set of events you may use to control the loading experience. The Web Font Loader is able to load fonts from Google Fonts, Typekit, Fonts.com, and Fontdeck, as well as self-hosted web fonts. It is co-developed by Google and Typekit.

like image 34
Mitul Avatar answered Sep 24 '22 05:09

Mitul