--- date_published: 2024-02-27 title: "Adding a Favicon in 2024" preview: "What do we actually need to do to add a favicon to a site in 2024? With a little Astro flavor." tags: ["web-dev"] --- Let's go on a quick side quest to add a favicon to this new site. These always seem to be a bit of a pain because the vendor requirements are always changing. Evil Martians have a really great writeup that covers a lot of this here: [How to Favicon in 2024: Six files that fit most needs ](https://evilmartians.com/chronicles/how-to-favicon-in-2021-six-files-that-fit-most-needs). We'll mostly follow that, with a few Astro-isms tossed in. To start, we'll need to create an asset for our favicon. I'd actually recommend calling this something other than `favicon` to prevent the browser from loading it in a way you don't expect, due to it looking for things named `favicon`. In the end, these are the assets I ended up with: ``` public/ apple-touch-icon.png // 180x180 for iOS favicon.ico // 32x32 for any non-svg supporting browsers // You may want a custom 16x16 icon if your // favicon won't look good scaled down automatically icon.svg // More on this below icon-dev.svg // Alternate icon to indicate development environments icon_192.png // 192x192 for Android icon_512.png // 512x512 for Android manifest.webmanifest // Web App Manifest ``` ## The SVG Favicon `icon.svg` does double duty as both the light and dark mode icon. ```html ``` We use a media query to change the fill color of the icon based on the user's preferred color scheme. This means that even if a user's browser uses a dark color tab bar, the favicon will still look good. This approach feels a bit more elegant than swapping out the icon with JavaScript, or having multiple icons in the markup. ## The Dev-Mode Favicon A cool little trick I picked up from the Evil Martians article is to use a different icon in development environments. The approach they used was a little different, involving a custom webpack plugin, but we can do something far easier in Astro. Here is my current `head.astro` file: ```astro --- interface HeadProps { title: string; } const is_dev = import.meta.env.DEV; const { title } = Astro.props as HeadProps; ---