【发布时间】:2013-06-27 06:09:43
【问题描述】:
我有一个网页,当我访问资料时会返回以下标题:
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
使用 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']
);
正确地将标头修改为这样的内容(基于 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
但根据我尝试检查的所有内容,我看不到任何证据表明这确实发生了:
-
cache不包含此文件的条目 -
Developer Console中的Network选项卡显示对 HTTP 响应没有任何更改(我尝试将其更改为甚至是微不足道的修改,只是为了确保它不是错误,但仍然没有更改)。
我能找到的唯一真正的提示是this question,这表明我的方法仍然有效,webRequest API documentation 上的这一段表明这不起作用(但没有解释为什么我不能得到任何变化):
请注意,网络请求 API 呈现了网络的抽象 堆栈到扩展。在内部,一个 URL 请求可以拆分为 几个 HTTP 请求(例如获取单个字节范围 从一个大文件)或可以由网络堆栈处理,而无需 与网络通信。因此,API 不 提供发送到网络的最终 HTTP 标头。为了 例如,所有与缓存相关的标头对 扩展名。
没有任何效果(我根本无法修改HTTP response header),所以我认为这是我最关心的问题。
关于我可能出错的地方或如何找到这里出错的任何建议?
如果不可能,还有其他方法可以实现我想要实现的目标吗?
【问题讨论】:
标签: google-chrome google-chrome-extension http-headers httpresponse cache-control