0

I've observed a very interesting behavior from Chrome that I do not observe from other browsers.

Chrome seems to cache downloads from the same URL on webservers hosted using Flask. For instance:

@app.route('/download/<string:filename>')
def download(filename):
    send_file(filename)

Essentially, if I visit localhost:3000/download/a_file.exe, then a_file.exe gets downloaded.

However, the problem is, if I modify a_file.exe (making it a new binary), then the old binary gets downloaded. Same goes for any file format, really. I originally found out this problem when I tried to replace a picture that I was attempting to download (let's say, hi.jpg). Even if I change hi.jpg to a new picture, the old one gets downloaded.

It gets even funnier.

If I delete the file, a_file.exe, from my project folder. Theoretically it should throw an error, right? Nope, still downloads the old a_file.exe, even if the file doesn't even exist.

If I stop the server, modify the download() function, restart the server

@app.route('/download/<string:filename>)
def download(filename):
   print("hi")
   send_file(filename)

Now what should happen is "hi" gets printed, but it doesn't, meaning Chrome basically completely bypasses actually sending a request to the server and just gives you the file.

The funniest thing is, even if completely shut down the server, it still downloads!!

On a serious note, this is pretty problematic (at least for me), as my server is essentially constantly modifying the same file and basically users should be able to get the most current version instead of just some random version they cached.

Again, this does not occur on other browsers, such as Edge (which also uses Chromium). I have tested myself.

If there is any solution that doesn't involve changing the file name itself (as that will probably solve it but is a bit problematic for me to implement), please let me know. Thanks in advance.

Leo Feng
  • 59
  • 8

1 Answers1

1

Yes, chrome has a highly robust cache. When testing, you can circumvent it by pressing CTRL+F5, which is reload with avoiding the cache.

You can also tell if you are hitting the cache, by looking at the server logs, and see if the endpoint was hit (similar to what you did with printing hi).

Also, in the chrome debug window, you can go to Network and see where the response came from (more here)

What can you do to avoid this?

Good news, the cache will expire at some point (When?).

Alternatively, if you can, you can set the Cache-Control header to

Cache-Control: no-cache

or

Cache-Control: max-age=0

Which should avoid caches.

Also, if caching is the behaviour you want, just not during development (which is likely the case if you want to avoid frequent file transfers over the network), you can disable caching in chrome as described here

tituszban
  • 4,797
  • 2
  • 19
  • 30