Question

I have a web page that returns the following header when I access material:

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

Using a chrome extension, I want to modify this response header so that the material is actually cached instead of wasting bandwidth.

I have the following sample code:

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']
);

Which correctly modifies the header to something like this (based on the console.log() output):

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

But based on everything I have tried to check this, I cannot see any evidence whatsoever that this has actually happened:

  1. The cache does not contain an entry for this file
  2. The Network tab in the Developer Console shows no change at all to the HTTP response (I have tried changing it to even trivial modifications just for the sake of ensuring that its not a error, but still no change).

The only real hints I can find are this question which suggests that my approach still works and this paragraph on the webRequest API documentation which suggests that this won't work (but doesn't explain why I can't get any changes whatsoever):

Note that the web request API presents an abstraction of the network stack to the extension. Internally, one URL request can be split into several HTTP requests (for example to fetch individual byte ranges from a large file) or can be handled by the network stack without communicating with the network. For this reason, the API does not provide the final HTTP headers that are sent to the network. For example, all headers that are related to caching are invisible to the extension.

Nothing is working whatsoever (I can't modify the HTTP response header at all) so I think that's my first concern.

Any suggestions at where I could be going wrong or how to go about finding what is going wrong here?

If its not possible, are there any other ways to achieve what I am trying to achieve?

Was it helpful?

Solution

I have recently spent some hours on trying to get a file cached, and discovered that the chrome.webRequest and chrome.declarativeWebRequest APIs cannot force resources to be cached. In no way.

The Cache-Control (and other) response headers can be changed, but it will only be visible in the getResponseHeader method. Not in the caching behaviour.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top