How can I make sure that static content is cached client-side?

前端 未结 2 416
终归单人心
终归单人心 2021-02-01 09:58

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

相关标签:
2条回答
  • 2021-02-01 10:41

    You can also make use of the HTML5 Offline web applications manifest. It allows you to set up a manifest where you define which files will be cached locally.

    It is a nice, clear to understand broadly implemented, way of avoiding having to learn about IIS and HTML Caching.

    http://www.w3schools.com/html/html5_app_cache.asp

    (you should totally read up about those things)

    0 讨论(0)
  • 2021-02-01 10:42

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

    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
    How to configure static content cache per folder and extension in IIS7?

    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).

    0 讨论(0)
提交回复
热议问题