Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

301 redirection doesn't update browser url #510

Closed
Tracked by #703
lucasflorian opened this issue Apr 8, 2020 · 14 comments · Fixed by #710
Closed
Tracked by #703

301 redirection doesn't update browser url #510

lucasflorian opened this issue Apr 8, 2020 · 14 comments · Fixed by #710
Assignees
Milestone

Comments

@lucasflorian
Copy link

Hi,

Using version 2.9.7

When receiving a 301 HTTP code from the server, the next page has the right content, but the browser URL is the requested url, not the response location header

I have a link that leads to :

https://domain.com/url

Server response is a HTTP 301 with location header :

https://domain.com/url/suburl

The content displayed by barba is the one from https://domain.com/url/suburl but the browser url is still https://domain.com/url

Thanks

@lucasflorian lucasflorian changed the title 301 redirection +doesn't update browser url 301 redirection doesn't update browser url Apr 8, 2020
@xavierfoucrier
Copy link
Member

xavierfoucrier commented Apr 8, 2020

Hi @lucasflorian,

Interesting case: I will look into it soonly.
Thanks for the issue 😉

@xavierfoucrier xavierfoucrier self-assigned this Apr 21, 2020
@xavierfoucrier
Copy link
Member

xavierfoucrier commented Apr 21, 2020

Hi @lucasflorian,

I can confirmed it's a bug, mostly an unsupported feature I mean 😄 !

Page is well prefetched/displayed, even with prefetchIgnore: true, but URL is not properly refreshed in the browser with the 301 targeted page URL...

Capture

For now, you can manually update the history in your code using:

barba.history.add('sub/suburl/');

See History class API for more informations.

@xavierfoucrier xavierfoucrier removed their assignment Apr 21, 2020
@thierrymichel thierrymichel self-assigned this May 28, 2020
@arun07as
Copy link

I've had the same issue ( just that in my case its a 302 as the page is only accessible to authenticated users ).

Is there any way I can get the response status code and the location header inside the barba hooks (or from anywhere else) so that I can check if there is a redirect, and then change the url to the Location header using the history api?

@xavierfoucrier
Copy link
Member

xavierfoucrier commented Jun 24, 2020

@johnrobertcobbold
Copy link

johnrobertcobbold commented Jul 20, 2020

@arun07as see https://barba.js.org/docs/advanced/recipes/#Browser-requests 😉

Hello Xavier,

I presume that you are referring to requestError in that link? We are having the same problem as @arun07as: some of our pages trigger a 302 redirect. Barba follows the redirect but does not update the url which is a problem for us.

On pages which require authentification, we have started returning a 401 error instead of a 302 redirection. This gets picked up by requestError so we can access the url that the user was trying to open, and forward some_parameters to a 401 page using something like barba.go('/401?some_parameters'). This is perfect because our 401 page can then use those parameters to redirect the user back to the original page upon sign-in. If we were not able to update the URL, this would not work.

On some other pages, we have genuine innocent 302 redirects and returning an 4xx does not make sense. For those pages, requestError does not trigger so we are stuck and cannot update the url. This is a problem as the user sometimes ends up with a url which is not valid. Would the only way around this is to be able to access the status code, even when there was no error?

@xavierfoucrier
Copy link
Member

Hi @goyavo,

Probably accessing the code will help developers deal with this kind of issue.
@thierrymichel has self-assigned and will fix this in a next release.

We are all a little busy right now, sorry for the delay.
Thanks for your patience 😉

@stale stale bot added the wontfix label Sep 19, 2020
@stale stale bot added the wontfix label Nov 21, 2020
@barbajs barbajs deleted a comment from stale bot Nov 24, 2020
@barbajs barbajs deleted a comment from stale bot Nov 24, 2020
@stale stale bot added the wontfix label Jan 23, 2021
@vkzawa
Copy link

vkzawa commented Apr 7, 2021

Thought I'd dig in on this and see if I could throw something together quickly but it looks like this would require a much bigger restructuring of the code to support 301/302 redirects than I expected.

I'll write up an outline of what I think needs to be done then maybe either @xavierfoucrier or @thierrymichel could review it.
If neither of you have started work by the time I'm ready to add this feature for our own projects this then I'll get started on a pull request for this.

I maintain a custom Express framework with which my team and I are building many sites (we've got 19 done and 30 or so lined up.) While I'd love to build our sites on something newer like Gatsby or Next.js that would be a huge migration for us, so Barba is a perfect solution for now to get a more modern single-page-app experience. We've already launched one site with Barba and we're looking to bake it into all future sites.

@barbajs barbajs deleted a comment from stale bot Apr 8, 2021
@fredy31
Copy link

fredy31 commented May 6, 2021

Any update on this? Working with Barba on a WP install I'm stuck if my 301's don't update the URL.

@fredy31
Copy link

fredy31 commented May 6, 2021

If others fall on that problem, here is a (very) dirty fix. I put the supposed page URL in the code, and check if both URLs match before showing the page. If the supposed URL is not the same as the JS readable URL (I've put that it only checks the last chunk, it should do) force a redirect of the page.

barba.init({
  transitions: [
    {
      name: "default-transition",
      leave(data) {
        return gsap.to(data.current.container, {
          opacity: 0,
        });
      },
      enter(data) {
        var supposedURL = jQuery(data.next.container).find('.js-variables-mule').attr('data-url');
        supposedURL = stripTrailingSlash(supposedURL).split('/');
        supposedURL = supposedURL[supposedURL.length - 1];
        var currURL = window.location + '';
        var currURLNoHash = currURL.split('#');
        currURLNoHash = currURLNoHash[0];
        currURLNoHash = stripTrailingSlash(currURLNoHash).split('/');
        currURLNoHash = currURLNoHash[currURLNoHash.length - 1];
        console.log(supposedURL +' '+ currURLNoHash);
        if(supposedURL != currURLNoHash){
          console.log('redirect');
          jQuery('#swup').css('display','none');
          jQuery('body,#swup').css('display','none');
          jQuery('body,#swup').css('opacity','0');
          window.location.href=currURL;
          return;
        }
        return gsap.to(data.next.container, {
          opacity: 1,
        });
      },
    },
  ],
})

function stripTrailingSlash(str) {
  if(str.substr(-1) === '/') {
      return str.substr(0, str.length - 1);
  }
  return str;
}

Like I said: its a VERY dirty fix, but it does work for now. It is also slower, because the page has to load first, check if its allright, and if it isn't do the reload.

@andersdn11
Copy link

Any progress on this? Still causing issues for me.

@xavierfoucrier
Copy link
Member

Hi everyone, unfortunately not.
We will work on barbajs on October/November now.

Thanks again for your patience.

@SamJol
Copy link

SamJol commented Feb 9, 2022

Any update on this? I'm having the same issue :(

@daun
Copy link

daun commented Jun 10, 2022

Both XMLHttpRequest and fetch support getting the final url after all redirects. See XMLHttpRequest.responseURL or Response.url. That could be used to update the current url and any cache entries. Especially for temporary 302 redirects, it'd be important not to cache the requested page but the final url.

I've tried monkeypatching barba's request helper, but haven't found an easy way to update all the places the href would need to be updated after the page has loaded and the final URL is known.

@xavierfoucrier xavierfoucrier added this to the barba@next milestone May 10, 2023
@xavierfoucrier xavierfoucrier self-assigned this May 12, 2023
@xavierfoucrier
Copy link
Member

Hi everyone 👋
Quick update: I will work on this in the next few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants