I am using HTML 5 history api to save state when ajax requests happen and i provide full html content if user request to same page with none ajax request.
"Re-open last closed tab" feature of browser brings last ajax request content without hitting to server. If browser would request without bring last request content then everything would work without problem. But browser just show last ajax request content.
I have been experienced this on Chrome 17, Firefox 10. (i haven't tried it on ie9 because it has no support history api)
What is well-known solution for this problem ?
Edit: These ajax requests are just "get" request to server.
it is really not possible to demonstrate it in jsfiddle.net because few reasons. You can demonstrate it in your localhost like below.
Make "get" request to server and pull json objects then push that url into history api like below.
history.pushState(null,null,url);
Then close that tab and click "Re-open last closed tab" feature of your browser. What do you see ? Json response body ? Browser shows it without making request to server, right ?
Chrome keeps the most recently closed tab just one click away. Right-click a blank space on the tab bar at the top of the window and choose Reopen closed tab. You can also use a keyboard shortcut to accomplish this: CTRL + Shift + T on a PC or Command + Shift + T on a Mac.
Answer: Click on the Chrome menu, Go to the History option, you will find an option with the number of tabs you have recently closed, click on it to undo closing all tabs and restore them.
Using a keyboard shortcut Use the following shortcut keys Command + Shift + T to reopen a closed tab or press Ctrl + Z.
Closed tab on accident You can simply right-click an empty area in the tab bar section and choose reopen closed tabs. You can also use a keyboard shortcut — press Ctrl+Shift+T (or Command+Shift+T on a Mac) and the last tab you closed will reopen in a new tab page.
When you reopen a closed tab, the browser is allowed to reuse the data from cache for the given URL to populate the window. Since the data in cache is from the ajax request response, that's what it uses, and you see the JSON.
So that leads to the question: Why didn't the browser use the HTML from cache when satisfying the ajax request? Browsers use different rules to determine whether to use cached content depending on what they're doing. In this case, it appears Chrome is happy to reuse it when restoring the recently-closed tab, and not when doing the ajax request.
You can correct it by telling the browser to never cache the response. Whether that's desirable depends on your use case.
For instance, inserting these at the top of your file (after the opening <?php
tag, of course) makes it not happen for me:
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
Problem was causing by http response headers. Headers was contain cacheable information for ajax requests so browser was showing that url content from cache without hit to database.
After removing cache params from response headers then browser was able to hit server without brings content from cache.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With