覆盖HTTP响应中的"缓存控制"值

B T*_*B T 8 google-chrome httpresponse cache-control http-headers google-chrome-extension

我有一个网页,当我访问材料时返回以下标题:

HTTP/1.1 200 OK
Date: Sat, 29 Jun 2013 15:57:25 GMT
Server: Apache
Content-Length: 2247515
Cache-Control: no-cache, no-store, must-revalidate, max-age=-1
Pragma: no-cache, no-store
Expires: -1
Connection: close
Run Code Online (Sandbox Code Playgroud)

使用chrome扩展,我想修改它,response header以便实际缓存材料而不是浪费带宽.

我有以下示例代码:

chrome.webRequest.onHeadersReceived.addListener(function(details) 
    {
        // Delete the required elements
        removeHeader(details.responseHeaders, 'pragma');
        removeHeader(details.responseHeaders, 'expires');

        // Modify cache-control
        updateHeader(details.responseHeaders, 'cache-control', 'max-age=3600;')

        console.log(details.url);
        console.log(details.responseHeaders);

        return{responseHeaders: details.responseHeaders};
    },
    {urls: ["<all_urls>"]}, ['blocking', 'responseHeaders']
);
Run Code Online (Sandbox Code Playgroud)

这正确地将标题修改为这样的东西(基于console.log()输出):

HTTP/1.1 200 OK
Date: Sat, 29 Jun 2013 15:57:25 GMT
Server: Apache
Content-Length: 2247515
Cache-Control: max-age=3600
Connection: close
Run Code Online (Sandbox Code Playgroud)

但基于我试图检查的所有内容,我无法看到任何证据确实发生了这种情况:

  1. cache不包含此文件中的条目
  2. 显示中的Network选项卡Developer Console对HTTP响应完全没有任何变化(我已经尝试将其更改为甚至是微不足道的修改,只是为了确保它不是错误,但仍然没有变化).

我能找到的唯一真实提示是这个问题表明我的方法仍然有效,而且这个段落在webRequest API文档中表明这不起作用(但不解释为什么我不能得到任何改变):

请注意,Web请求API将网络堆栈的抽象呈现给扩展.在内部,一个URL请求可以分成几个HTTP请求(例如,从大文件中获取单个字节范围),或者可以由网络堆栈处理,而无需与网络通信.因此,API不提供发送到网络的最终HTTP标头.例如,与缓存相关的所有标头对于扩展都是不可见的.

没有什么是可行的(我根本无法修改HTTP response header)所以我认为这是我的第一个担忧.

我可能出错的地方或如何找到问题在这里的任何建议?

如果它不可能,有没有其他方法来实现我想要实现的目标?

Rob*_*b W 5

我最近花了几个小时尝试缓存文件,并发现API chrome.webRequestchrome.declarativeWebRequestAPI 无法强制缓存资源.绝不.

Cache-Control(和其他)响应头可以改变,但它只会在可见光getResponseHeader方法.不在缓存行为中.