"Open last closed tab" to display the last content of the ajax request

I use the HTML 5 history api to maintain state when making ajax requests, and I provide full html content if the user request to the same page without any ajax request.

The function "Open last closed tab" of the browser brings the last content of the ajax request without getting to the server. If the browser will request without adding the last contents of the request, everything will work without problems. But the browser just shows the latest ajax request content.

I tested this on Chrome 17, Firefox 10. (I have not tried it on ie9 because it does not have api support history)

What is a well-known solution to this problem?

Edit: these ajax requests are just a get request to the server.

it is really impossible to demonstrate this in jsfiddle.net because there are few reasons. You can demonstrate this in your local host, as shown below.

Make a “get” request to the server and pull out the json objects, then click this url into the api history as shown below.

history.pushState(null,null,url); 

Then close this tab and click "Reopen the last closed tab" in your browser. What do you see? Json response body? The browser shows this without a server request, does it?

+6
source share
3 answers

The problem was caused by HTTP response headers. The headers contained cached information for ajax requests, so the browser showed this URL from the cache without getting into the database.

After removing the cache settings from the response headers, the browser was able to get to the server without bringing the contents from the cache.

+4
source

When you reopen a closed tab, the browser is allowed to reuse cache data for the specified URL to fill the window. Since the data in the cache is related to the response to the ajax request, what it uses is and you see JSON.

So this leads to the question: why didn't the browser use HTML from the cache when executing the ajax request? Browsers use different rules to determine whether to use cached content depending on what they do. In this case, Chrome will be happy to reuse it when restoring a closed tab, and not when executing an ajax request.

You can fix this by telling the browser to never cache the response. Is this desirable from your use.

For example, pasting them at the top of your file (after opening the <?php tag, of course) does not do this 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"); 
+1
source

It all depends on which browser you use and which optimizations are included.

Google Chrome, for example, will save the page in memory, so when you click back and go to a new site or when you open the closed tab again, it will restore the page from memory.

Old / slow browsers will just update something.

Although this should not be a problem, since your javascript state must also be restored - it should be exactly the same in all cases when they reopen this page.

0
source

Source: https://habr.com/ru/post/909350/


All Articles