Explain Codes LogoExplain Codes Logo

How to prevent an HTTP request just for a favicon?

web-development
performance
best-practices
responsive-design
Nikita BarsukovbyNikita Barsukov·Aug 31, 2024
TLDR

To prevent an HTTP request just for a favicon, consider embedding a base64-encoded transparent image directly into the HTML <link> tag:

<link rel="icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/w8AAusB9FD6P9cAAAAASUVORK5CYII=" type="image/png">

By incorporating this into the <head> of your HTML, you'll stop the browser from fetching the favicon, successfully saving an HTTP request.

Making the most of cache and sprites

Leveraging browser caching

For repeated visits, the browser can cache the favicon to skip future HTTP requests. Caching benefits more if the favicon remains the same for the majority of user interactions.

<!-- Browser, remember this fellow for next time! --> <link rel="icon" href="/path/to/favicon.ico" type="image/x-icon">

Merging favicons in CSS sprites

The favicon can join other small images on your website in a group outing, or in other words, a single sprite. Using a single sprite image and CSS, display the suitable section for each image:

.favicon { /* Say cheese! Our favicon's in the group photo */ background: url('/path/to/sprite.png') no-repeat -16px -16px; width: 16px; height: 16px; }

Embed the class in an element like <span class="favicon"></span> to serve the favicon, sans an extra HTTP request.

Clever embedding and browser harmony

Leveraging inline SVGs

Inlining the favicon as an SVG data URI provides scalability, reduces request size, and permits dynamic manipulation. This results in sharper visuals without additional HTTP requests.

<!-- Doing yoga: Flexible, scalable, and incredibly sharp --> <link rel="icon" href="data:image/svg+xml,<svg ...></svg>" type="image/svg+xml">

Supporting all browsers

Your optimization should cater to all browsers. Older versions may raise their brows at modern optimizations, hence include appropriate fallbacks. Placing the traditional favicon.ico in the root directory generally works as a steadfast fallback.

Advanced tricks and precautions

Implementing JavaScript Injection

A dynamic favicon achieved through JavaScript allows advanced manipulation while avoiding extraneous HTTP requests.

/* Presto chango! Your favicon just learned a new trick */ let link = document.createElement('link'); link.rel = 'icon'; link.href = 'data:image/svg+xml;base64,...'; // base64-encoded SVG or PNG document.head.appendChild(link);

Your favicon can now change dynamically based on user interaction or other conditions.

Weighing performance and novelty

Avoid going overboard while optimizing your favicon at the cost of bigger performance gains. Ensure to assess the impact of each optimization in relation to other potential site improvements.

Experimenting with Emoji Favicons

An emoji as an inventive SVG favicon adds a unique touch and keeps your request light. Just keep in mind the desired look and feel of your brand before adopting this approach.

Prioritizing practicality and user experience

Optimizations should boost user experience. Ensure your favicon optimizations do not compromise the overall performance and aesthetic of your website.