Blog About Contact

Wicket - forcing page reload on browser back button...

Published Tue, 23 Mar 2010 • 14 comments

An interesting issue arises when doing AJAX with Wicket, where you have an AJAX-enabled page with a bunch of controls that get generated/refreshed by AJAX. The user then navigates to another page, and subsequently hits the back button and returns the the AJAX page. They click a button and boom! the app explodes, with a "can not find component foo on page bar" error.

The reason this occurs is because most browsers cache the initial version of a page. When the user hits the back button, the browser displays the cached "initial" page. Wicket on the other hand still stores the page state as it was before the user navigated to the subsequent page. When the user clicks a button on the cached page, the component they are clicking on most likely doesn't exist in the server-side page... hence the boom!

A simple solution to this is to force page reloading on the use of the back button. Add this into a base page class (or pages where you want to force this behaviour):

class MyPage extends WebPage {
   ......
   @Override
   protected void configureResponse() {
       super.configureResponse();
       WebResponse response = getWebRequestCycle().getWebResponse();
       response.setHeader("Cache-Control", 
             "no-cache, max-age=0,must-revalidate, no-store");
   }
   .....
}

I'd be interested in other solutions to this problem that aren't quite so "brute force". I'm thinking something along the lines of a "DOM ready" javascript hook which causes a synchronisation of the AJAX modified elements of the page on reload. I can see there would be a few issues to iron out with that approach though.

So for now the above looks like a decent workaround.


About the Author

Richard Nichols is an Australian software engineer with a passion for making things.

Follow him on twitter or subscribe by RSS or email.

You might also enjoy reading -


Discuss / Comment

There are 14 comments.

Add a comment

  • {{e.error}}

Thanks for your comment!/

Required.
Valid email address required.
Required.
Posting message, please wait...