0

I'm using scala 2.11.8 and Play Framework 2.3. I am submitting a form and managing the result through a controller. The controller returns a Redirect to a page flashing a certain message, either a success or an error message:

Redirect(routes.MyController.page).flashing("success" -> "All went well")

I then manage this message using javascript, so it can be seen in a modal:

@flash.get("success").map { message =>
    <script type="text/javascript">
        modalSuccess('@message');
    </script>
}

All goes well, the modal is shown. Next I navigate to another page and use the Back arrow to go back and the modal is shown again. This is, of course, very weird for the user: he has just clicked back, it makes no sense that he sees a "All went well" message.

Previously, I passed these messages as parametres to the pages and returned a 200 OK:

Ok(views.html.page(message = "All went well"))

But the exact same thing happened because the request was sent again. I switched to Flash to solve this, but I can't seem to prevent it from showing when returning to the previous page.

Should I clear the Flash scope, or should it clear by itself, once the redirect is executed?

VC.One
  • 12,230
  • 4
  • 21
  • 51

1 Answers1

0

When you click back, by default the browser will serve the page out of its cache, it won't reissue the request, and that is what is happening here, it has nothing to do with Play.

There is no standard for how the browser back button behaves, in particular with regards to caching, so solving it isn't straight forward because all the browsers behave differently. This SO question/answer covers it fairly comprehensively:

How to control web page caching, across all browsers?

But be aware of the drawbacks, if you tell the browser it must never cache pages, you can end up giving users a poor experience, for example, if they are filling out a form, and they accidentally navigate away, and then click back so they can continue filling out the form, if you force the browser now to cache the page they will lose everything that they entered into the form.

Also, what if the reason they are clicking back is that they want to see the rendered model again? When users click back, they expect to see the page that they were just on, just as it was. If you change that, you're breaking their expectations. I wouldn't be happy if that happened to me - I hate it when I click back and I get a different page, you're probably going to upset your users if you do it.

James Roper
  • 12,340
  • 41
  • 44