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

Nick Ginanto picture Nick Ginanto · Mar 13, 2013 · Viewed 16.8k times · Source

I am using this type of line in an JS response

if (history && history.pushState){
     history.pushState(null, null, '<%=j home_path %>');
}

but when I click back in the browser, I see the JS code of the response instead of the previous page.

Is there a way to make the back button work so it would re-request the page of the last url (before home_path as pushStated in?

Update:

I've found this.. it seems other have the same issue with.. History.js doesn't fix it either

So to confirm some of the comments there.. This happens only in chrome

What I think

I think the root of all this is that the popstate requests the same type of the pushstate document (js response). What needs to be done is on popstate request the html response.. I just don't know how

More updates:

seeing http://railscasts.com/episodes/246-ajax-history-state it mentions to use

 $(window).bind("popstate", function() {
      $.getScript(location.href);
    });

this solves the issue but according to understanding jQuery $.getScript() withtout ajax caching it will add timestamps.. which pollutes the url.. turning it off makes it not work again with the same effect..

Anyone knows how to solves this?

Even more Updates

I tracked the trails of the issue all over the interweb and ended with an issue in chrome which points to a rails issue (firefox works fine however) and an issue in rails-ujs concerning not including Vary Header in the responses

Example can be found here

Answer

Nick Ginanto picture Nick Ginanto · Apr 4, 2013

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