The other day I was reminded of two neat tricks related to customizing your favicon to bring some delight to your website visitors and also to improve your development flow - all thanks to scottykaye!
If you want to skip right to the code, checkout both recipes in my hamlim/custom-favicon-recipes repo!
Customizing Favicons for Light and Dark Mode
The first recipe is a neat way to change the favicon of your site based on the preferred color scheme of the visitor. You can use the <link />
tag's media
attribute
to have the browser "choose" the preferred icon based on light or dark mode.
If you're writing plain old html (or using some framework that gives you control over the html), you can use the below snippet to accomplish this:
If you're using Next.js and it's metadata
or generateMetadata
exports, you can do the following:
You can see this recipe in action on this Next app!
Customizing Favicons for Development and Production
This is a pretty neat trick that makes it just a bit easier to determine if you're looking at the development version of your site or the production one.
We've all been there, we're about 45 minutes into trying to debug an issue, you keep making more basic and basic changes to the code, refreshing the page in the browser and still can't see your changes. Only to then realize that you've been madly refreshing the production site expecting to find your local changes.
With a custom icon for development, you'll no longer get stuck in that situation again!
If you're using Next.js and it's metadata
or generateMetadata
exports, you can do the following:
You can see this recipe in action on this other Next app!
Let me know if you know of other tips and tricks with customizing your site's favicon!