librelist archives

« back to archive

Prevent XMLHttpRequest caching (in IE)

Prevent XMLHttpRequest caching (in IE)

From:
Malphas Wats
Date:
2012-05-29 @ 12:20
Hi,

  I have a function that returns a bit of data as a JSON string, it
looked like this:

    @mod.route('/file/last_uploaded')
    def get_last_uploaded_file():
        ... get file id ...
        return jsonify(file_url = url_for('files.url', file_id=file_id))

  which worked perfectly well all morning whilst I was developing in
Chrome. When I tested in IE, it did the old "Cache it and never
request it again" trick, which is annoying.

I tried this:

    from werkzeug.datastructures import Headers

    @mod.route('/file/last_uploaded')
    def get_last_uploaded_file():
        ... get file id ...
        h = Headers()
        h.add('Expires', "Mon, 26 Jul 1997 05:00:00 GMT")
        h.add('Cache-Control', "no-cache, must-revalidate")
        h.add('Pragma', "no-cache")
        r = jsonify(file_url = url_for('files.url', file_id=file_id))
        return Response(r, headers=h)

which I thought was rather clever, however, I get "TypeError:
'Response' object is not iterable", which made me feel decidedly less
clever.

I'd really rather not append an ugly timestamp query string to the request url.

What's the best way to achieve non-caching XMLHttpRequest in Internet Explorer?

Thanks

Re: [flask] Prevent XMLHttpRequest caching (in IE)

From:
Todd Kennedy
Date:
2012-05-29 @ 12:55
Setting no-cache headers aggressively on your response.

I'm on my way to work but I'll send the list we use when I arrive.

IE has massively aggressive caching

On May 29, 2012, at 8:22, Malphas Wats <malphas@subdimension.co.uk> wrote:

> Hi,
>
>  I have a function that returns a bit of data as a JSON string, it
> looked like this:
>
>    @mod.route('/file/last_uploaded')
>    def get_last_uploaded_file():
>        ... get file id ...
>        return jsonify(file_url = url_for('files.url', file_id=file_id))
>
>  which worked perfectly well all morning whilst I was developing in
> Chrome. When I tested in IE, it did the old "Cache it and never
> request it again" trick, which is annoying.
>
> I tried this:
>
>    from werkzeug.datastructures import Headers
>
>    @mod.route('/file/last_uploaded')
>    def get_last_uploaded_file():
>        ... get file id ...
>        h = Headers()
>        h.add('Expires', "Mon, 26 Jul 1997 05:00:00 GMT")
>        h.add('Cache-Control', "no-cache, must-revalidate")
>        h.add('Pragma', "no-cache")
>        r = jsonify(file_url = url_for('files.url', file_id=file_id))
>        return Response(r, headers=h)
>
> which I thought was rather clever, however, I get "TypeError:
> 'Response' object is not iterable", which made me feel decidedly less
> clever.
>
> I'd really rather not append an ugly timestamp query string to the request url.
>
> What's the best way to achieve non-caching XMLHttpRequest in Internet Explorer?
>
> Thanks

Re: [flask] Prevent XMLHttpRequest caching (in IE)

From:
Simon Sapin
Date:
2012-05-29 @ 13:46
Le 29/05/2012 14:20, Malphas Wats a écrit :
>          h = Headers()
>          h.add('Expires', "Mon, 26 Jul 1997 05:00:00 GMT")
>          h.add('Cache-Control', "no-cache, must-revalidate")
>          h.add('Pragma', "no-cache")
>          r = jsonify(file_url = url_for('files.url', file_id=file_id))
>          return Response(r, headers=h)
>
> which I thought was rather clever, however, I get "TypeError:
> 'Response' object is not iterable", which made me feel decidedly less
> clever.


jsonify’s return value is already a Response object. Try something like 
this:

r = jsonify(...)
r.headers.add('Expires', '...')
return r

-- 
Simon Sapin

Re: [flask] Prevent XMLHttpRequest caching (in IE)

From:
Todd Kennedy
Date:
2012-05-29 @ 14:00
We find that the following set of header (seriously) are the only way to 
get IE9 to return to the server every time:

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

It looks ridiculous, but IE no longer caches...

On May 29, 2012, at 9:46 AM, Simon Sapin wrote:

> Le 29/05/2012 14:20, Malphas Wats a écrit :
>>         h = Headers()
>>         h.add('Expires', "Mon, 26 Jul 1997 05:00:00 GMT")
>>         h.add('Cache-Control', "no-cache, must-revalidate")
>>         h.add('Pragma', "no-cache")
>>         r = jsonify(file_url = url_for('files.url', file_id=file_id))
>>         return Response(r, headers=h)
>> 
>> which I thought was rather clever, however, I get "TypeError:
>> 'Response' object is not iterable", which made me feel decidedly less
>> clever.
> 
> 
> jsonify’s return value is already a Response object. Try something like 
> this:
> 
> r = jsonify(...)
> r.headers.add('Expires', '...')
> return r
> 
> -- 
> Simon Sapin