librelist archives

« back to archive

Add no-cache to all POST responses

Add no-cache to all POST responses

From:
David Small
Date:
2012-10-07 @ 11:13
Hi All

In our application we make great use of Flask POST to support AJAX calls. 
The recent change in iOS 6 has caused these responses to be cached in the 
IOS device so that only the first response is ever displayed. Imagine a 
clock that tells you the time and then never changes (until you manually 
empty the cache in your IOS device).

I found Armin's reply at 
http://librelist.com/browser//flask/2011/8/8/add-no-cache-to-response/#952cc027cf22800312168250e59bade4
and implemented the @nocache decorator which solves the problem. However 
it means adding this decorator to every POST route.

I was wondering if there is a way of automatically adding a 'no-cache' 
header to all POST response objects.

It is hard to think of a case when this would be harmful.

Thanks

david
--
David Small
Oxford UK

Re: [flask] Add no-cache to all POST responses

From:
Simon Sapin
Date:
2012-10-07 @ 12:18
Le 07/10/2012 13:13, David Small a écrit :
> Hi All
>
> In our application we make great use of Flask POST to support AJAX
> calls. The recent change in iOS 6 has caused these responses to be
> cached in the IOS device so that only the first response is ever
> displayed. Imagine a clock that tells you the time and then never
> changes (until you manually empty the cache in your IOS device).
>
> I found Armin's reply at
> 
http://librelist.com/browser//flask/2011/8/8/add-no-cache-to-response/#952cc027cf22800312168250e59bade4
and
> implemented the @nocache decorator which solves the problem. However it
> means adding this decorator to every POST route.
>
> I was wondering if there is a way of automatically adding a 'no-cache'
> header to all POST response objects.
>
> It is hard to think of a case when this would be harmful.

Hi,

This sounds like a job for an after_request function:
http://flask.pocoo.org/docs/api/#flask.Flask.after_request


     from flask import request
     @app.after_request
     def add_no_cache(response):
         if request.method == 'POST':
             response.cache_control.no_cache = True
         return response


Cheers,
-- 
Simon Sapin

Re: [flask] Add no-cache to all POST responses

From:
David Small
Date:
2012-10-07 @ 13:44
On 7 Oct 2012, at 13:18, Simon Sapin wrote:

> Le 07/10/2012 13:13, David Small a écrit :
>> Hi All
>> 
>> In our application we make great use of Flask POST to support AJAX
>> calls. The recent change in iOS 6 has caused these responses to be
>> cached in the IOS device so that only the first response is ever
>> displayed. Imagine a clock that tells you the time and then never
>> changes (until you manually empty the cache in your IOS device).
>> 
>> I found Armin's reply at
>> 
http://librelist.com/browser//flask/2011/8/8/add-no-cache-to-response/#952cc027cf22800312168250e59bade4
and
>> implemented the @nocache decorator which solves the problem. However it
>> means adding this decorator to every POST route.
>> 
>> I was wondering if there is a way of automatically adding a 'no-cache'
>> header to all POST response objects.
>> 
>> It is hard to think of a case when this would be harmful.
> 
> Hi,
> 
> This sounds like a job for an after_request function:
> http://flask.pocoo.org/docs/api/#flask.Flask.after_request
> 
> 
>     from flask import request
>     @app.after_request
>     def add_no_cache(response):
>         if request.method == 'POST':
>             response.cache_control.no_cache = True
>         return response
> 
> 
> Cheers,
> -- 
> Simon Sapin

Hi Simon

A perfect solution which resolves the iOS 6 "feature" (of caching POST 
requests) in six lines.

Thanks very much

david
--
David Small
Oxford UK