如何阻止仅针对 favicon 的 HTTP 请求?
每个人都知道如何在 HTML 中设置 favicon.ico 链接:
<link rel="shortcut icon" href="http://hi.org/icon.ico" type="image/x-icon">
但愚蠢的是,我们只需要一个几个字节的小图标又一个可能影响速度的 HTTP 请求。
所以我想知道,如何才能使该图标成为可用精灵的一部分(例如,background-position=0px -200px;
),同时兼作网站其余部分的徽标,例如为了加快网站速度并节省宝贵的 HTTP 请求。我们如何才能将其与我们的徽标和其他艺术品一起放入现有的精灵图像中?
Everybody knows how to set up a favicon.ico link in their HTML:
<link rel="shortcut icon" href="http://hi.org/icon.ico" type="image/x-icon">
But it's silly that for only a several-byte-tiny icon we need yet yet another potentially speed-penalizing HTTP request.
So I wondered, how could I make that favicon part of a usable sprite (e.g., background-position=0px -200px;
) that doubles as, say, a logo on the rest of the website, in order to speed up the site and save that precious and valuable HTTP request. How can we get this to go into an existing sprite image along with our logo and other artworks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我认为在大多数情况下,它不会导致另一个 HTTP 请求,因为这些请求通常在第一次访问后转储到浏览器的缓存中。
这实际上比任何建议的“解决方案”都更有效。
I think for the most part it does not result in another HTTP request as these are usually dumped in the browser's cache after the first access.
This is actually more efficient than any of the proposed "solutions".
对 @yc 的答案 是从通常会使用和缓存的 JavaScript 文件注入 Base64 编码的 favicon,并且还抑制请求
favicon.ico 通过在相关
meta
标记中为其提供 数据 URI 。此技术避免了额外的 http 请求,并被证实可以在 Windows 7 上的最新版本的 Chrome、Firefox 和 Opera 中使用。但是,它似乎至少在 Internet Explorer 9 中无法工作。
文件 index.html
文件 script.js
演示:turi .co/up/favicon.html
A minor improvement to @yc's answer is injecting the Base64-encoded favicon from a JavaScript file that would normally be used and cached anyway, and also suppressing the standard browser behavior of requesting
favicon.ico
by feeding it a data URI in the relevantmeta
tag.This technique avoids the extra http request and is confirmed to work in recent versions of Chrome, Firefox and Opera on Windows 7. However it doesn't appear to work in Internet Explorer 9 at least.
File index.html
File script.js
Demo: turi.co/up/favicon.html
您可以尝试数据 URI。没有 HTTP 请求!
除非您的页面具有静态缓存,否则您的网站图标将无法被缓存,并且根据您的网站图标图像的大小,您的源代码可能会因此而变得臃肿。
数据 URI 图标似乎适用于大多数现代浏览器;我可以在 Mac 上最新版本的 Chrome、Firefox 和 Safari 中使用它。似乎不适用于 Internet Explorer,也可能不适用于某些版本的 Opera。
如果您担心旧的 Internet Explorer 版本(现在您可能不应该担心),您可以添加一个 Internet Explorer 条件注释,该注释将在传统的浏览器中加载实际的 favicon.ico方式,因为旧版 Internet Explorer 似乎不支持数据 URI 图标。
您还可以只使用另一个可能已缓存其网站图标的热门网站的网站图标,例如
http://google.com/favicon.ico
,以便从缓存中提供服务。正如评论者所指出的,仅仅因为您可以这样做并不意味着您应该这样做,因为无论我们设计什么技巧,某些浏览器都会请求 favicon.ico。与通过 gzip 压缩、对静态内容使用远期过期标头、缩小 JavaScript 文件、将背景图像放入精灵或数据 URI 等操作所节省的开销相比,这样做所节省的开销是微不足道的、通过 CDN 提供静态文件等。
You could try a data URI. No HTTP request!
Unless your pages have static caching, your favicon wouldn't be able to be cached, and depending on the size of your favicon image, your source code could get kind of bloated as a result.
Data URI favicons seems to work in most modern browsers; I have it working in recent versions of Chrome, Firefox and Safari on a Mac. Doesn't seem to work in Internet Explorer, and possibly some versions of Opera.
If you're worried about old Internet Explorer versions (and you probably shouldn't be these days), you could include an Internet Explorer conditional comment that would load the actual favicon.ico in the traditional way, since it seems that older Internet Explorer doesn't support data URI favicons.
You could also just use the favicon of another popular site which is likely to have their favicon cached, like
http://google.com/favicon.ico
, so that it is served from cache.As commenters have pointed out, just because you can do this doesn't mean you should, since some browsers will request favicon.ico regardless of the tricks we devise. The amount of overhead you'd save by doing this would be minuscule compared to the savings you'd get from doing things like gzipping, using far-future expires headers for static content, minifying JavaScript files, putting background images into sprites or data URIs, serving static files off of a CDN, etc.
2020 年的杀手级解决方案
该解决方案必然会在问题最初提出九年后出现,因为直到最近,大多数浏览器还无法处理
.svg
格式的网站图标。现在情况已不再是这样了。
请参阅:https://caniuse.com/#feat=link-icon-svg
1) 选择 SVG 作为 Favicon 格式
现在,即 2020 年 6 月,这些浏览器可以处理 SVG Favicons:
请注意,这些浏览器仍然不能:
尽管如此,考虑到上述情况,我们现在可以以合理的信心使用SVG Favicons。
2) 将 SVG 显示为数据 URL
这里的主要目标是避免 HTTP 请求。
正如本页上的其他解决方案所提到的,一种非常聪明的方法是使用数据 URL 而不是 HTTP URL。
SVG(尤其是小型 SVG)完美适合数据 URL,因为后者只是纯文本(任何可能不明确的字符都经过百分比编码),而前者是 XML,可以写为长文本一行明文(带有少量百分比代码)非常简单。
3) 整个 SVG 是一个表情符号
2019 年 12 月,Leandro Linares 是最早意识到 Chrome 与 Firefox 一起支持 SVG Favicons 的人之一,因此值得尝试看看是否可以从表情符号创建一个 Favicon:
https://lean8086.com/articles/using-an-emoji- as-favicon-with-svg/
Linares 的预感是正确的。
几个月后(2020 年 3 月),Code Pirate Lea Verou 意识到了同样的事情:
https ://twitter.com/leaverou/status/1241619866475474946
网站图标也不再一样了。
4) 自己实现解决方案:
这是一个简单的 SVG:
Killer Solution in 2020
This solution necessarily comes nine years after the question was originally asked, because, until fairly recently, most browsers have not been able to handle favicons in
.svg
format.That's not the case anymore.
See: https://caniuse.com/#feat=link-icon-svg
1) Choose SVG as the Favicon format
Right now, in June 2020, these browsers can handle SVG Favicons:
Note that these browsers still can't:
Nevertheless, with the above in mind, we can now use SVG Favicons with a reasonable degree of confidence.
2) Present the SVG as a Data URL
The main objective here is to avoid HTTP Requests.
As other solutions on this page have mentioned, a pretty smart way to do this is to use a Data URL rather than an HTTP URL.
SVGs (especially small SVGs) lend themselves perfectly to Data URLs, because the latter is simply plaintext (with any potentially ambiguous characters percentage-encoded) and the former, being XML, can be written out as a long line of plaintext (with a smattering of percentage codes) incredibly straightforwardly.
3) The entire SVG is a single Emoji
In December 2019, Leandro Linares was one of the first to realise that since Chrome had joined Firefox in supporting SVG Favicons, it was worth experimenting to see if a favicon could be created out of an emoji:
https://lean8086.com/articles/using-an-emoji-as-favicon-with-svg/
Linares' hunch was right.
Several months later (March 2020), Code Pirate Lea Verou realised the same thing:
https://twitter.com/leaverou/status/1241619866475474946
And favicons were never the same again.
4) Implementing the solution yourself:
Here's a simple SVG:
And here's the same SVG as a Data URL:
And, finally, here's that Data URL as a Favicon:
5) More tricks (...these are not your parents' favicons!)
Since the Favicon is an SVG, any number of filter effects (both SVG and CSS) can be applied to it.
For instance, alongside the White Unicorn Favicon above, we can easily make a Black Unicorn Favicon by applying the filter:
Black Unicorn Favicon:
您可以使用 Base64 编码的图标,例如:
You could use a Base64-encoded favicon, like:
我在此页面上找到了一个有趣的解决方案。它是德语的,但您将能够理解代码。
您将图标的 base64 数据放入外部样式表中,因此它将被缓存。在网站的头部,您必须使用 id 定义 favicon,并将该 favicon 设置为该 id 样式表中的背景图像。
和html
I found an interesting solution on this page. It is in German, but you will be able to understand the code.
You put the base64 data of the icon into an external style sheet, so it will be cached. In the head of your website you have to define the favicon with an id and the favicon is set as a
background-image
in the style sheet for that id.and the html
好点,好主意,但不可能。网站图标必须是一个单独的资源。无法将其与其他图像文件合并。
Good point and nice idea, but impossible. A favicon needs to be a single, separate resource. There is no way to combine it with another image file.
这真的重要吗?
许多浏览器将图标作为低优先级加载,这样它就不会阻止页面加载,所以是的,这是一个额外的请求,但它不在任何关键路径上。
JavaScript 解决方案很糟糕,因为 JavaScript 代码已被检索并执行,下面的所有 DOM 元素都将被阻止渲染,并且不会减少请求数量!
Does it really matter?
Many browsers load the favicon as a low priority so that it doesn't block the page load in anyway, so yes it's an extra request, but it's not on any critical path.
A JavaScript solution is horrible because JavaScript code has been retrieved and executed, all the DOM elements below will be blocked from rendering and it doesn't reduce the number of requests!
正确的解决方案是使用 HTTP 管道。
要求服务器支持,但不一定参与。
许多浏览器客户端在应该这样做的时候却没有这样做。
我建议你尝试在 Firefox 中启用管道并在那里尝试,或者只使用 Opera(不寒而栗)。
The proper solution is to use HTTP pipelining.
It's required that servers support it, but not necessarily participate.
Many browser clients don't do it, when they should.
I would recommend you try enabling pipelining in Firefox and try it there, or just use Opera (shudder).
这并不是真正问题的答案,而只是为了补充 Marcel 和 yahelc。我为 404 favicon 问题提供了一个优雅的解决方案。
某些应用程序和浏览器会检查 favicon.ico 文件,如果在站点根目录中找不到该图标,您只需使用 204 响应 标头。
Apache 示例:
Apache 选项一(也是我最喜欢的),.htacces 或 .conf 中的简单一行:
Apache 选项二:
为了进一步阅读,有一个很好的 Stoyan Stefanov 的博客文章。
This is not really an answer to the question, but simply to compliment the answers given by Marcel and yahelc. I offer an elegant solution to the 404 favicon issue.
Some applications and browsers check for a favicon.ico file and if the icon is not found in the site root, you can simply respond to the request with the 204 response header.
Apache Examples:
Apache option one (and my favorite), a simple one-liner in your .htacces or .conf:
Apache option two:
For further reading there is a nice blog post by Stoyan Stefanov.
这是一个好主意,但如果谷歌还没有在他们的主页上做到这一点,我敢打赌它(目前)无法做到。
It's a great idea, but if Google hasn't done it on their homepage, I'm betting it can't (currently) be done.
抱歉,您无法将该图标与其他资源结合起来。
这意味着您基本上有两个选择:
如果您对没有网站图标的网站感到满意 - 您可以将
href
指向已加载的非图标资源(例如,样式表、脚本文件,甚至是一些受益于预取的资源)。(我的简短测试表明这适用于大多数(如果不是全部)主要浏览器。)
接受额外的 HTTP 请求,并确保您的 favicon 文件设置了积极的 HTTP 缓存控制标头。
(如果您控制着其他网站,您甚至可能让他们偷偷地预加载该网站的图标 - 以及其他静态资源。)
PS 创意解决方案 不起作用:
元素的延迟注入(如 由用户 yc 建议)可能只会让事情变得更糟 - 导致两个 HTTP 请求。
I'm sorry, but you can't combine the favicon with another resource.
This means you have basically two options:
If you're comfortable with your site not having a favicon - you can just have the
href
point to a non-icon resource that is already being loaded (e.g., a style sheet, script file, or even some resource that benefits from being pre-fetched).(My brief testing indicates that this works across most, if not all, major browsers.)
Accept the extra HTTP request and just make sure your favicon file has aggressive HTTP cache-control headers set.
(If you have other websites under your control, you might even have them sneakily preload the favicon for this website - along with other static resources.)
P.S. Creative solutions that will not work:
<link>
element (as suggested by user yc) will likely just make things worse - by resulting in two HTTP requests.您可以使用 8 位 PNG 图像代替 ICO 格式,以获得更小的数据占用空间。您唯一需要更改的是使用“data:image/png”而不是“data:image/x-icon” MIME 类型标头:
“type”属性可以是“image/png”或“image/x-icon” 。两者都适合我。
您可以使用 GIMP 或
convert
将 ICO 转换为 8 位 PNG:并使用
base64
命令将 PNG 二进制文件编码为 Base64 字符串:You can use an 8-bit PNG image instead of the ICO format for an even smaller data footprint. The only thing you have to change is using "data:image/png" instead of "data:image/x-icon" MIME type header:
"type" attribute can be "image/png" or "image/x-icon". Both work for me.
You can convert ICO to 8-bit PNG using GIMP or
convert
:And encode the PNG binary to a Base64-string using the
base64
command:这是最简单的方法:
它代表什么图标?下面回答一下!
Here's the easiest way:
What icon does it represent? Answer below!