Stop browser to make HTTP requests for images that should stay cached - mod_expires

后端 未结 10 1022
猫巷女王i
猫巷女王i 2020-12-04 10:01

After reading many articles and some questions on here, I finally succeded in activating the Apache mod_expires to tell the browser it MUST cache images

10条回答
  •  离开以前
    2020-12-04 10:37

    The behavior you are seeing is the intended (see RFC7234 for more details), specified behavior:

    All modern browsers will send HTTP requests to the server for every page element displayed, regardless of cache status. This was a design decision made at the request of web services (especially advertising networks) to ensure that HTTP servers were able to maintain records of every display of every element.

    If the browsers did not make these requests, the server would never be notified that an image had been displayed to the user. For advertising networks, this would be catastrophic. Early on, advertising networks 'hacked' their way around this by serving the same ad image using randomly generated names (ex: 'coke_ad_1_98719283719283.gif'). However, for ISPs this practice caused a huge increase in data transfers, because every one of their users was re-downloading these identical ad images, bypassing any caching/proxy servers their ISP was operating.

    So a truce was reached: Browsers would always send HTTP requests, even for un-expired cached elements. Servers would respond with HTTP 304 status codes ("not modified"). This allows the servers to record the fact that the image was displayed to the client. As a result, advertising networks generally stopped using randomized image names to bypass network cache servers.

    This gave the ad networks what they wanted - a record of every image displayed - and it gave ISPs what they wanted - cache-able images and static content.

    That is why there isn't much you can do to prevent browsers from sending HTTP requests for cached page elements.

    But if you look at other available client-side solutions that came along with html5, there is a scope to prevent resource loading

    1. Cache Manifest (in spite of its gotchas)
    2. IndexedDB (nice asynchronous features, allows blob storage)
    3. Local Storage (not async)

提交回复
热议问题