【问题标题】:Caching local files缓存本地文件
【发布时间】:2015-10-04 17:38:08
【问题描述】:

我使用PrivoxyProxomitron 将自定义Javascript标签注入网站,然后从本地python服务器(在localhost:8888上)加载脚本:

... <script type="text/javascript"
src="http://localhost:8888/tweakscript.js"
></script></body>

其中一些脚本标签是巨大的第三方 javascript 库,它们也存储在我的计算机上。它们永远不会改变,但每次都会重新加载。我想缓存它们。

我尝试了这些标题,但没有成功:

HTTP/1.1 200 OK
Content-type: text/javascript; charset=UTF-8
Vary: Accept-Encoding
Date: Sun, 04 Oct 2015 03:24:14 GMT                # the current date
Last-Modified: Fri, 02 Oct 2015 06:34:40 GMT       # never changes
Expires: Fri, 01 Apr 2016 03:24:14 GMT             # one month in future
Cache-Control: public, max-age=15552000            # cache for one year
Access-Control-Allow-Origin: *                     # Content Security Policy
Access-Control-Allow-Methods: GET, POST, OPTIONS
Connection: close

(javascript code here)

如何让浏览器缓存这些文件?

【问题讨论】:

    标签: python-3.x localhost http-caching


    【解决方案1】:

    好的,我找到了解决方案:

    在请求文件时,浏览器会添加一个If-Modified-Since请求头,例如:

    If-Modified-Since: Tue, 28 Jul 2015 22:48:42 GMT
    

    如果服务器随后作为响应发送 ...

    HTTP/1.1 304 Not Modified
    

    ...然后浏览器将从缓存中加载文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 2012-10-07
      • 1970-01-01
      • 2021-08-15
      • 2022-06-17
      • 1970-01-01
      • 2015-07-23
      相关资源
      最近更新 更多