Tuesday, July 28, 2015

API for Retrieving Website Favicon with Fallback

I have worked on a number of projects where pulling in favicons from external websites needed to be done. This would typically be approached by checking for existence of a /favicon.ico in the root of the website or something along those lines. This isn't fool proof by any means though since the favicon can be named differently or located in a different path. It also takes a lot of unnecessary overhead to check for existence of a favicon before serving up a fallback.

Fortunately, there is a "hidden" Google API of sorts that handles this for you. It is used to pull in favicons for websites attached to your Google+ profile page. It works as follows:

http://s2.googleusercontent.com/s2/favicons?domain=mrrobwad.blogspot.com&alt=p

It turns out the fallback can be one of a few things. So far, I've discovered that setting the 'alt' parameter to 's' yields a blank page icon and 'p' yields a page template icon when no favicon is available. Setting to any other single letter yields a browser/world icon. There could be more, but I have only checked for single letters so far.

Enjoy!

No comments:

Post a Comment