CSS文件的浏览器缓存 [英] Browser Caching of CSS files

查看:130
本文介绍了CSS文件的浏览器缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于CSS和浏览器的快速问题。我尝试搜索SO,发现了一些类似的帖子,但没有确定的。



我在我的web项目中使用一个或两个CSS文件。这些都在我的网页的HEAD中引用。一旦我点击我的一个页面,CSS是否获得缓存,以便它不会重新下载与每个请求?我希望如此。做IE,Firefox和Safari处理这个不同?如果浏览器关闭,是否在打开新的浏览器实例时首次访问时刷新CSS?

解决方案

可能会被缓存 - 但它依赖...



不同的浏览器具有略微不同的行为 - 最明显的是当处理歧义/从服务器。如果你发送一个清晰的信号,浏览器几乎所有的时间。



到目前为止最大的差异是在不同的Web服务器和应用程序的默认缓存配置服务器。



一些(例如Apache)可能提供已知的静态文件类型,使用HTTP标头鼓励浏览器缓存它们,而其他服务器可能会发送

...



因此,首先,请阅读一些出色的 HTTP缓存教程 HTTP缓存和缓存内容发布商的缓存清除
对我来说是一个真正的开瓶器: - )



下一步安装并使用 Firebug Live HTTP Headers add-on,以找出您的服务器实际发送的标头。



然后阅读您的网络服务器文档,以了解如何调整他们的完美(或者说话系统管理员为您做)。



...

关于浏览器重新启动时会发生什么,它取决于浏览器和用户配置。



经验法则,希望浏览器在每次重新启动后更有可能检查服务器,看看是否有任何改变(参见 If-Last-Modified and If-None-Match )。



如果正确配置服务器,它应该能够返回超短 304未修改(成本非常高小带宽),然后浏览器将正常使用缓存。


Quick question regarding CSS and the browser. I tried searching SO and found some similar posts, but nothing definitive.

I use one or two CSS files in my web projects. These are referenced in the HEAD of my web pages. Once I hit one of my pages, does the CSS get cached so that it's not re-downloaded with each request? I hope so. Do IE, Firefox and Safari handle this differently? If the browser is closed, is the CSS refreshed on the first visit when a new browser instance is opened?

解决方案

Your file will probably be cached - but it depends...

Different browsers have slightly different behaviors - most noticeably when dealing with ambiguous/limited caching headers emanating from the server. If you send a clear signal, the browsers obey, virtually all of the time.

The greatest variance by far, is in the default caching configuration of different web servers and application servers.

Some (e.g. Apache) are likely to serve known static file types with HTTP headers encouraging the browser to cache them, while other servers may send no-cache commands with every response - regardless of filetype.

...

So, first off, read some of the excellent HTTP caching tutorials out there. HTTP Caching & Cache-Busting for Content Publishers was a real eye opener for me :-)

Next install and fiddle around with Firebug and the Live HTTP Headers add-on , to find out which headers your server is actually sending.

Then read your web server docs to find out how to tweak them to perfection (or talk your sysadmin into doing it for you).

...

As to what happens when the browser is restarted, it depends on the browser and the user configuration.

As a rule of thumb, expect the browser to be more likely to check in with the server after each restart, to see if anything has changed (see If-Last-Modified and If-None-Match).

If you configure your server correctly, it should be able to return a super-short 304 Not Modified (costing very little bandwidth) and after that the browser will use the cache as normal.

这篇关于CSS文件的浏览器缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆