handleResponse causing problems with history #167

Closed
opened 2018-06-19 09:03:08 -05:00 by piperhaywood · 4 comments
piperhaywood commented 2018-06-19 09:03:08 -05:00 (Migrated from github.com)

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!

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 2018-06-19 09:15:44 -05:00 (Migrated from github.com)

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

Can you post a link to website or codepen demonstrating the issue?
piperhaywood commented 2018-06-19 09:21:21 -05:00 (Migrated from github.com)

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!

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 2018-06-19 10:43:30 -05:00 (Migrated from github.com)

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

Leave it open. I'll try to test it at some point.
BehindTheMath commented 2018-07-22 17:38:46 -05:00 (Migrated from github.com)

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.

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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: iLoveElysia/pjax#167