back button in browser not working properly after using pushState (in Chrome) back button in browser not working properly after using pushState (in Chrome) google-chrome google-chrome

back button in browser not working properly after using pushState (in Chrome)


I am currently playing around with doing

 response.headers['Vary'] = 'Accept'

which seems to solve the problem, at first look.

If anyone has the same issue, please check it and let me know


You need to add a listener to the popstate event

window.onpopstate = function(event) {    //load the page};

When the back button is pressed the url is changed and then the popstate event is fired. It's your responsibility to load the details in your popstate listener that match the changed url.

A nice popstate example

Some browsers fire a popstate event when the page first loads, causing an infinite page loading loop. This can be avoided by adding the following check

var loadPage = window.history.state;window.onpopstate = function(event) {    if (loadPage)         //load the page};

Then set the loadPage to true when pushState is called

history.pushState(null, null, '<%=j home_path %>');loadPage = true;

This technique works in all browsers that support html5 history api.


It happened my with sinatra & chrome.

Solved it with:

$.ajaxSetup({ cache: false });