Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can web font loader provide local fallback if a CDN font fails? #172

Closed
sciascia opened this issue Dec 27, 2013 · 7 comments
Closed

Can web font loader provide local fallback if a CDN font fails? #172

sciascia opened this issue Dec 27, 2013 · 7 comments

Comments

@sciascia
Copy link

I'm sorry to be posting here but I can't find any info I understand - I have the following which works great:

  <script src="//ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js"></script>
  <script>
    WebFont.load({
      google: {
        families: ['Flamenco'],
      },
      custom: {
        families: ['FontAwesome'],
        urls: [ '//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css' ]
      }
    });
  </script>

But how do I use the .wf-fontawesome-n4-inactive class that gets added to the html tag to conditionally load a local version? Obviously .wf-fontawesome-n4-inactive @font-face {} makes no sense.

Any pointers in the right direction would be much appreciated.

Cheers

@bramstein
Copy link
Contributor

You can use the callbacks to load another font if the first one fails. In this case you would listen for the fontinactive event and check if the name of the font matches the one you want to load a fallback for:

WebFont.load({
  fontinactive: function (family, fvd) {
    if (family === 'FontAwesome') {
      WebFont.load({
          ...
      })
    }
  },
  custom: {
    families: ['FontAweome'],
    urls: ['//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css']
  }
});

Let me know if you have any other questions.

@sciascia
Copy link
Author

Awesome, thank you so much.

Just one last thing, how would I test to see if the actual WebFont library has loaded?

I've tried a bunch of stuff and this seems to work but it takes about 5 seconds before the local fonts display so am not sure if this is right.

Or is window a jQuery function so there's a delay while jQuery loads locally?

Hope that makes sense - sorry if this is easy.

<script src="//ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js"></script>
<script>window.WebFont || document.write('<script src="assets/js/webfont.js"><\/script>')</script>
<script>
  WebFont.load({
    fontinactive: function (family, fvd) {
      if (family === 'Flamenco' && 'FontAwesome') {
        WebFont.load({
          custom: {
            families: ['Flamenco', 'FontAwesome'],
            urls: [ '/assets/fonts/local-fonts.css' ]
          }
        })
      }
    },
    google: {
      families: ['Flamenco'],
    },
    custom: {
      families: ['FontAwesome'],
      urls: [ '//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css' ]
    }
  });
</script>

@bramstein
Copy link
Contributor

I think your current approach is OK, although I don't think it is necessary to provide a fallback for the script. The Google CDN is used by millions of websites and highly reliable (and so is their web font service.)

@sciascia
Copy link
Author

sciascia commented Jan 3, 2014

Thanks for that, I realise Google's CDN is unlikely to go down but I've had this issue using jQuery from a CDN in the past (once) - the client wasn't happy!

Cheers

Ben

@sciascia sciascia closed this as completed Jan 3, 2014
@ghost
Copy link

ghost commented Aug 5, 2014

Stumbled upon this looking for a solution to a circumstance for when the very reliable Google CDN doesn’t work: in China!

@adamwintle
Copy link

I just stumbled upon this too 👍 using this method as a fall back to local fonts for sites with heavy China traffic

@BenceSzalai
Copy link

if (family === 'Flamenco' && 'FontAwesome') {

This should probably be more like:

 if (family === 'Flamenco' || family === 'FontAwesome') {

However since these are all the fonts defined, the whole conditional can be omitted and WebFont.load(... called directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants