Search code examples
yii2pjax

Pushstate in pjax only when redirecting


In one of my pages I have a link that is handled by pjax. Basically the user clicks an item, this item becomes "checked" (and saved in the DB).

I have disabled pushState for these requests, because it makes no sense, user effectively stays in the same page, so it's counter-intuitive to change the url.

However, there is a case when this pjax request results in redirect to login page (when the user is not logged in). And this is when I really need pushState to work, and it doesn't because I disabled it in the first place.

Would it be possible to configure pjax in such a way that normal responses work without pushState, but redirect responses (done with X-Pjax-Url header) do perform pushState?


Solution

  • There's no way to do it using current functionality. I've added two more options to pjax and my PR has been accepted to yii2 branch of pjax. So, without further ado:

    https://github.com/yiisoft/jquery-pjax

    //pushRedirect - Whether to pushState the URL for redirects. Defaults to false.
    //replaceRedirect - Whether to replaceState the URL for redirects. Defaults to true.
    
    // ...
    
    jQuery(document).pjax("#example_selector", {
      "push": false,
      "replace": false,
      "pushRedirect": true,
      "replaceRedirect": false
    });