Search code examples
python-3.xlocalhosthttp-caching

Caching local files


I use Privoxy or Proxomitron to inject custom Javascript tags into websites which then load scripts from a local python server (on localhost:8888):

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

Some of these script tags are huge third party javascript libraries which are also stored on my computer. They never change but are reloaded each time. I want to cache them.

i tried these headers, without success:

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)

How can i make the webbrowser cache these files?


Solution

  • OK, i found the solution:

    When requesting files, the browser adds an If-Modified-Since request header, eg:

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

    If the server then sends as response ...

    HTTP/1.1 304 Not Modified
    

    ... then the browser will load the file from cache.