1

This is what I have tried from Python/tornadoweb:

    self.set_header("Cache-Control","no-cache, must-revalidate, max-age=0")
    self.set_header("Expires","Mon, 26 Jul 1997 05:00:00 GMT")

This is what I see from firebug when I first load the page:

Cache-Control   no-cache, must-revalidate, max-age=0
Content-Length  1715
Content-Type    text/html; charset=UTF-8
Etag    "e55dc7115d80aa09b470510ababb3515706f4a61"
Expires Mon, 26 Jul 1997 05:00:00 GMT
Server  TornadoServer/2.3
Set-Cookie  xsfr=5b7f3cf86c2e4537acd1bb1749484a5b; Path=/

And yet, when I press BACK button to go back to the original URL, I get a cached version of the page! The page is not re-fetched from the server. The result is that it contains invalid hidden form values. No matter how the user fills in the form, it cannot be processed.

The problem can be reproduced on firefox and chrome, but not from internet explorer.

So, how to force firefox and chrome to disable the cache and reload the page whenever the back button is pressed?

nagylzs
  • 3,954
  • 6
  • 39
  • 70
  • Assuming you're trying to prevent people from resubmitting a form you can have the POST the form, do whatever you want them to do and then instead of outputting the HTML content there, redirect to a different page (can be static or something dynamic based on the form). That way when they hit back, the redirect should be reprocessed instead of the previous page. – Cfreak Sep 05 '13 at 18:57
  • Yes, this is a well known trick to prevent users from resubmitting forms. But it simply makes the BACK button useless. I want to keep the functionality of the BACK button. All I ask is to force the browser to reload the page with it hits the same URL again. And I think this is what cache control is for. – nagylzs Sep 05 '13 at 19:01
  • You could redirect them back to the same page. That would force it to reload. Still though however you do it, if that page reloads, any user interaction (like if they filled out a form) will be lost. – Cfreak Sep 05 '13 at 19:12
  • I'm sorry I'm not sure what you mean. #1. page A - the form that should not be cached #2. page B is the target of the form, it should process the form, and at the end instead of writting HTML redirect to page C #3. page C should write out the HTML (meaning, the HTML should be stored between the two last requests?) #4. Then when the user presses the BACK button, then the browser asks if you want to repost the data to page B. – nagylzs Sep 05 '13 at 19:25
  • Sorry I wasn't clear. You could redirect the user to the form again (as page C). Give them a message that their data was updated but repopulate it in the fields so if they want to submit again they can. Without knowing what your form consists of it's hard for me say if that's a good idea or not. However I am confused, are you wanting the user to see the Repost Data alert or are you trying to avoid it? – Cfreak Sep 05 '13 at 22:04
  • I thought of something else: if you were completely set on the back button basically refreshing your form you could have page A submit to B and B just outputs the HTML normally. Page A could have Javascript that resets the field values. Even if the page doesn't reload from the server the Javascript should execute again. It's not perfect but it might get you closer to what you're looking for – Cfreak Sep 05 '13 at 22:06
  • I do not want the user to see the repost data alert. What I want is to go back to "page A" with the BACK button. But instead of loading it from the cache, load it from the server (beacuse it is expired). But I do not want to force the user to go BACK. Here is what I need: #1 page A loaded from server, form displayed #2 form POST-ed to page B, where the user sees the result #3. when the user presses BACK then browser reloads page A without asking, but the user is not forced to press BACK in any way. – nagylzs Sep 06 '13 at 05:52
  • For your second comment, yes I could have a javascript that resets values. The very first reason I wanted the form to be reloaded from the server is because values of hidden fields are coming from the server and they cannot be calculated/reset from javascript. And those hidden values can only be used once. For the next post, the user needs to get new values from the server. But maybe it it not possible to control cache for pages accessed by the BACK button? I'll have to find a workaround. – nagylzs Sep 06 '13 at 05:59
  • I just realized that my problem has almost nothing to do with forms. My main problem is that the BACK button always returns to a cached version of the page. It does not really matter if that page contains a form or not. So even if the page is just a simple static page, it should be reloaded, right? Because it is expired. – nagylzs Sep 06 '13 at 22:17

1 Answers1

0

I don't know if you solved this issue or not, but I faced the same issue last night. This answer helped me to some extent. I solved it by setting the header and clearing the user cookie.

Here's a gist of what I did :

class BaseHandler(tornado.web.RequestHandler):

    def set_default_headers(self):
        self.set_header('Cache-Control', 'no-cache, no-store, must-revalidate')
        self.set_header('Pragma', 'no-cache')
        self.set_header('Expires', '0')

Now the SignOut handler :

class SignOut(BaseHandler):

    def get(self):
        self.clear_cookie("user")
        self.redirect('/')

"user" is the name of the cookie set.

Community
  • 1
  • 1
Shivam Mutreja
  • 45
  • 1
  • 10