4

I put some files in static directory and these files are keep updated by some back end processing. I used url_for() to return url to client side.

But I found that even if I removed the old files from static directory and regenerate the updated files, it still display the content of old files on the client browser. I think the old file has been cached.

Does anyone know how to set no-cache for the url_for() function?

Thanks in advance!

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
vycon
  • 139
  • 2
  • 12
  • 1
    Are you positive? Press `CTRL-SHIFT-R` on your keyboard to forcibly reload the page. – Blender Aug 16 '12 at 23:56
  • Thanks for reply. But I dont want to reload the whole page. I just used ajax to refresh parts (e.g., iframe) of the page – vycon Aug 17 '12 at 00:03
  • 1
    If you're using jQuery, disable caching: http://stackoverflow.com/questions/168963/stop-jquery-load-response-from-being-cached – Blender Aug 17 '12 at 00:04

2 Answers2

1

I wrote a blog post about this a while ago.

Basically, you need to create a response to send, then set some extra headers on the response, then send the response:

def send_url(method):

    response = app.make_response(url_for(method))

    response.headers.add('Last-Modified', datetime.datetime.now())
    response.headers.add('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0')
    response.headers.add('Pragma', 'no-cache')

    return response
MalphasWats
  • 3,255
  • 6
  • 34
  • 40
0

Disabling all caching just because once in a while you have a new release is really not good practice. Caching of css and js gives huge performance benefits.

What good sites do is to add a timestamp of the file to the resource URLs. For example, in Flask, here is a way to do this transparently: http://flask.pocoo.org/snippets/40/

patb
  • 1,688
  • 1
  • 17
  • 21