Why? ๐ค
If you have spent more than a minute on this website, you'll know that I like putting icons and emojis in text. ๐ They make everything more fun and serve as a visual aid to quickly identify links and topics. ๐ง
Now, I could go and manually download and resize every single favicon I want to use, but that would be a lot of work. ๐ฉ Instead, I fetch them dynamically at build time, convert them to .webp
and serve them statically.
Look at them go! ๐
Facebook
The easy way ๐ฅฒ
Having favicons show up next to links could be soo easy:
<a href="https://kosro.de">
Click me
<img src="http://www.google.com/s2/favicons?domain=kosro.de" />
</a>
This works thanks to Google's internal favicon API. There's also some other services that offer this, like DuckDuckGo and Yandex:
https://www.google.com/s2/favicons?domain=kosro.de?sz=64
https://icons.duckduckgo.com/ip2/kosro.de.ico
https://favicon.yandex.net/favicon/v2/kosro.de/reddit.com
Keep in mind that these services are not officially documented and could be taken down at any time.
GDPR Compliance ๐ช๐บ
The GDPR
As IP addresses are considered personal data, explicit consent is required before any requests may be made to a server in the USA.
This means that directly using most favicon APIs is a no-go for any website that wants to be GDPR compliant. Shucks. ๐ฉ
The hard way ๐ชจ
Luckily, Nuxt Image
Awesome! So I can just use the Google API and be done with it, right?
Nope.
Turns out, Nuxt Image
WHICH IS MENTIONED EXACTLY 0 TIMES IN THE DOCS ๐คฌ
At least there's this issue
Unfortunately, most favicon APIs require query parameters to pass the URL or desired size.
DuckDuckGo.ico
format, which IPX
Making it pretty โจ
So far so good. Next problem:
Some icons are black. The website's background is black.
Enter fast-average-color
I register a callback for when the icon is loaded (or mounted in case of prerendered pages
And that's it!