MoOx / pjax

Easily enable fast Ajax navigation on any website (using pushState + xhr)
MIT License
1.43k stars 124 forks source link

handleResponse causing problems with history #167

Closed piperhaywood closed 6 years ago

piperhaywood commented 6 years ago

I’m trying to get a custom response handler working and am running in to what I think may be a bug. It may well be that I'm doing something wrong though!

As a reduced test case, I've tested this out with the handleResponse example from the read me, as below:

var pjax = new Pjax();

pjax._handleResponse = pjax.handleResponse;

pjax.handleResponse = function(responseText, request, href) {
  if (request.responseText.match("<html")) {
    pjax._handleResponse(responseText, request, href);
  } else {
    // handle response here
  }
}

The behaviour is great, as expected, when you click around the links on the site. It is also fine for the first "back" click (popstate event) in the browser. Subsequent popstate events, however, return the same responseText, request, and href every time, as if it’s stuck on the same page.

Is there something I'm missing, maybe something that I should be resetting after a successful pjax event? Any help you could provide would be really, really appreciated!

BehindTheMath commented 6 years ago

Can you post a link to website or codepen demonstrating the issue?

piperhaywood commented 6 years ago

I don’t have a website I can demonstrate it on just yet unfortunately and can’t figure out how to do this via Codepen since it involves history... I actually just figured out that I was drastically overcomplicating things by trying to create a custom response handler so am no longer using this method.

Regardless, once I get this online I’ll try to do a demo of the behaviour above in case it is useful but for now, might be best to close it since I’m not sure how I can usefully demonstrate it to you. Very sorry for the faff!

BehindTheMath commented 6 years ago

Leave it open. I'll try to test it at some point.

BehindTheMath commented 6 years ago

This bug is being caused by a mistake in the README, and as a result, the code snippet in the first post. The handleResponse function needs to be passed an options parameter, otherwise it has no way to know if it came from a popstate event, and it will end up pushing the current state again.