如何确保静态内容缓存在客户端? [英] How can I make sure that static content is cached client-side?

查看:154
本文介绍了如何确保静态内容缓存在客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确保缓存静态内容(图像,css,javascript)?什么是最好的方法?

How can I make sure that static content (images, css, javascript) is cached? What is the best approach?

推荐答案

建议你通过本教程,了解缓存如何在Web(HTTP)上发生。

Will recommend you to go through this tutorial to understand how caching happens on web (HTTP) in general.

简单地说, Web服务器需要在将内容发送到客户端时生成适当的HTTP头,以便控制客户端缓存。在ASP.NET / IIS环境中,其IIS通常处理静态文件内容,因此,您必须根据需要适当地配置IIS以控制缓存静态文件。有关为静态内容配置IIS缓存的详细信息,请参阅以下链接:

Simply speaking, the web server needs to generate appropriate HTTP headers while sending the content to the client in order to control client-side caching. In ASP.NET/IIS environment, its IIS that typically handles the static file contents and therefore, you must configure IIS appropriately to control caching static files as per you needs. See below links for more information about configuring IIS caching for static content:

http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache

如何在IIS7中为每个文件夹和扩展配置静态内容缓存?

编辑:正如您所询问的最佳方法,我现在看到的最流行的方法是版本静态内容(例如在文件或URL的末尾添加一些版本标识符。版本编辑后,您可以将其视为不可变,然后发出缓存标头以便无限期地缓存它。在ASP.NET应用程序中,您可以将程序集版本(或产品版本)附加到每个静态内容URL。基本上,您将使每个构建(或每个产品版本)的缓存无效。

EDIT: As you have asked about the best approach, the most prevalent approach that I see now days is to version static content (say by appending some version identifier at the end of file or URL). Once version-ed, you can treat it as immutable and then emit cache headers for caching it for infinite duration. In ASP.NET application, you can probably append the assembly version (or product version) to each static content URL. So essentially, you will invalidating the cache for every build (or every product release).

这篇关于如何确保静态内容缓存在客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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