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

Popstate events doesn't trigger transition correctly with query string #587

Closed
Tracked by #703
BigGitWorld opened this issue Nov 16, 2020 · 21 comments
Closed
Tracked by #703
Assignees
Milestone

Comments

@BigGitWorld
Copy link

BigGitWorld commented Nov 16, 2020

Hi.
Barba version: lastest release version
Browser: firefox and Chrome (Latest versions)

How I added barba:
I have wrapped body of page () with the data-barba="wrapper" and main content of pages (div) with the data-barba="container" data-barba-namespace="home" and content for different pages are changing properly.

Scenario:
When I want to move between screens in a page with pagination items, back/forward button does not change content of page.
For example:

  1. http://localhost:8080/ -> main page, I click a button in this page to send a request to server to open page (2)
  2. http://localhost:8080/products?page=1 -> a simple page with paginated items with page=1
  3. https://localhost:8080/products?page=2 -> the above simple page with paginated items with page=2

For the first time when I click step (1)-(3), I send a request to the server to fetch data and everything works.
Also, when I click on the buttons (1),(2) in the paginated page (step 2 and 3), content of page changes.

Error:
Now I want to use back/forward button to move between steps (1), (2) and (3).
moving between (1) and (2) works, but moving between (2) and (3) does not work (only URL of page changes and content of shown paginated items does not change).

what is the problem?
When I use jquery-pjax library, everything woks fine by back/forward buttons.
I used the barba.js, because it says that it uses pjax and history API that is something I need.
I'm looking for a solution to send request to the server or use client side only to move between pages.

@xavierfoucrier xavierfoucrier self-assigned this Nov 16, 2020
@xavierfoucrier
Copy link
Member

Hi @BigGitWorld,

Thanks for opening the issue.
Please provide a minimum reproductible test repository for that specific case.

Regards 😉

@xavierfoucrier xavierfoucrier added the await test repository waiting for a minimal test repository label Nov 16, 2020
@BigGitWorld
Copy link
Author

Dear @xavierfoucrier
I'm sorry really for late answer. Please excuse me because I'm very busy.
But I will provide a simple example as soon as possible.
so please don't close the issue.

@xavierfoucrier
Copy link
Member

@BigGitWorld I am busy too, so take your time 😉

@pburdylo
Copy link

pburdylo commented Jan 4, 2021

I think I can provide an example, I stumbled the same behavior.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body data-barba="wrapper">
    <div data-barba="container">
        <a href="?step=1">step 1</a><br>
        <a href="?step=2">step 2</a><br>
        <a href="?step=3">step 3</a><br>
        <a href="?step=4">step 4</a><br>
    </div>
    <script src="https://unpkg.com/@barba/core"></script>
    <script>
        barba.init({
            transitions: [
                {
                    name: 'generic',
                    enter() {
                        console.log('generic enter');
                    },
                }
            ]        
        })
    </script>
</body>
</html>

Navigating through the links works fine (a few 'enter' logs). When I click back and/or forward button I don't get any log, seems nothing changes except the url.

@xavierfoucrier
Copy link
Member

Hi @pburdylo!

Thanks for your feedback.
I will have a look at this soon 😉

@xavierfoucrier xavierfoucrier changed the title Back forward button does not work correctly Back forward button does not work correctly with query string Jan 9, 2021
@xavierfoucrier
Copy link
Member

Hi @pburdylo @BigGitWorld,

Just to let you know, this is a duplicate of #532 .. 😃

Barba is made for page transitions, this is the normal behavior since query parameters are not considered as "pages".

But anyway, it looks a little bit buggy after a bunch of tests.
Here is a quick workaround that uses the magic self transition:

barba.init({
  transitions: [{
    name: 'generic',
    enter() {
      console.log('[generic] enter');
    },
  }, {
    name: 'self',
    enter() {
      console.log('[self] enter');
    },
  }],
});

There are disparities when only using the generic or self transition alone.

I keep the issue opened in order to discuss it with @thierrymichel.

@xavierfoucrier xavierfoucrier added @barba/core discussion duplicate and removed await test repository waiting for a minimal test repository labels Jan 9, 2021
@pburdylo
Copy link

pburdylo commented Jan 11, 2021

My problem is that the transition works with query params when using links ('a href') but doesn't when using back/forward.

BTW – are transition names ('generic', 'self') important or are they only informational and can be anything? I assumed the latter, but then your example shouldn't work (no difference between generic and self but for the name), which made me reconsider...

@xavierfoucrier
Copy link
Member

@pburdylo self is a magic keyword, see https://barba.js.org/docs/advanced/transitions/#Self.

@pburdylo
Copy link

still, the situation is confusing.

  1. when initializing barba with only 'generic' transition the behavior is (a) transition on links leading to the same page with different query args (b) reload on link to the same page with the same query args (c) no transition / reload when using BACK/FWD buttons (only URL is changed)
  2. when initializing barba with both 'generic' and 'self' transitions the behavior I get is (a) generic transition on links leading to the same page with different query args (b) self transition on link to the same page with the same query args (c) self transition when using BACK/FWD buttons

I expect same behavior when using BACK/FWD or clicking on a link when the clicked link is the same as the link in browser history stack. What do you think?

PS – thanks for staying in touch, great job with the lib :)

@pburdylo
Copy link

three more cents:

  1. a workaround in a simple case is to have two transitions (generic & self) reusing the same code
  2. not considering query args as page changes is a bit over restricting IMO. (a) paged product list as in original GreatGitWorld post is a good example (b) my real case is language change (c) in general – standard wordpress configuration gets you xxx.com/?page_id=ID... I guess the list could be easily extended.
  3. is it possible to have a few 'self' transitions with custom rules? My fast experiment shows that 'custom' property is ignored on self transition. And my intuition says that transition names should be unique. That would be pretty standard situation for me to have a specific animation for different cases, like reload of home page and loading next product list page, etc.

@xavierfoucrier
Copy link
Member

Hi @pburdylo,

Can you take the time to build a minimal reproductible test repository?
This way Thierry will be able to have a look when coming back to this issue 😉

I already noticed that this is a little bit buggy.
Great thanks!

@pburdylo
Copy link

scenario 1 – just a plain index.html file with the code I pasted above #587 (comment)

scenario 2 – the same file with barba.init replaced with the code you pasted above #587 (comment)

@pburdylo
Copy link

do you have any update on that?

@xavierfoucrier
Copy link
Member

Hi @pburdylo,

Sorry but for now I haven't found the time to dive into this bug..
Thanks for your patience and have a nice day! 🌞

@stale stale bot added the wontfix label Jun 9, 2021
@martinkosmela
Copy link

Hi,
I'm here because the same issues with back/forward buttons occured also to me, and it seems that the bug hasn't been resolved yet.

I experienced the problem while filtering the page by using query params.

My current implementation of barba (2.9.7) init looks like below:

barba.init({
    debug: true,
    prefetchIgnore: true,
    cacheIgnore: true,
    transitions: [
        {
            name: 'self',
            before(data) {
                console.log('before self', data);
            },
            enter() {
                console.log('enter self');
            },
            after() {
                console.log('after self');
            }
        }
    ],
});

barba.hooks.before(data => {
    console.log('before hook');
});

barba.hooks.enter(() => {
    console.log('enter hook');
});

barba.hooks.after(() => {
    console.log('after hook');
});

There is one scenario, where everything looks to be working fine:

  1. http://localhost:3000/reports - opens a page with list of items
  2. http://localhost:3000/reports?document-type=type-1 - send request and filter documents by type 1

Now, after clicking back button, all hooks getting fired and page contents reloads to previous state without query params as expected.

image

Another scenario:

  1. http://localhost:3000/reports - opens a page with list of items
  2. http://localhost:3000/reports?document-type=type-1 - send request and filter documents by type 1
  3. http://localhost:3000/reports?document-type=type-2 - send request and filter documents by type 2

Now, after clicking back button, "before" hooks gets fired and barba error is displayed in console. URL in the browser change to the previous one, but content of the page do not update.

image

@xavierfoucrier is it any chance for you to find the time to dive into this bug, please?
What could be causing the error at @barba/core/dist/barba.umd.js shown on the screenshot above?

@barbajs barbajs deleted a comment from stale bot Dec 31, 2021
@xavierfoucrier
Copy link
Member

@martinkosmela I will. I am currently in vacation mode with my family. Thanks for your patience, and again, sorry for the delay.

@martinkosmela
Copy link

@xavierfoucrier I hope you have had a great time vacationing with your family.

Did you have any chance to take a look on this issue since then?

@xavierfoucrier
Copy link
Member

@martinkosmela I am currently on a rush period right now, but, we take time with Thierry to discuss on current bugs and opened issues.

This one is assign to me, and I will take the appropriate time to check it as soon as possible.
I also have code example from @pburdylo to debug.

Thanks again for your patience.

@braican
Copy link

braican commented May 16, 2022

I'm also seeing a similar issue where I'm getting the same TypeError: Cannot read properties of undefined (reading 'undefined') error as @martinkosmela upon trying to use the back button to return to a page that contains a query string.

I have an application that is fetching and rendering records from an API with filter controls available to the user. When a user interacts with a filter, I'm modifying the current history state with history.replaceState to add the active filters to the url via a query string and triggering another request to the API to fetch the filtered results. An example flow might look like this:

  1. A user lands on the initial page at https://localhost/courses
  2. In the filter UI, they'll click on Monday to filter results by those courses that only happen on a Monday. This action will trigger two things:
    • We'll use history.replaceState to update the url to https://localhost/courses?day=monday
    • An ajax request gets fired off to fetch all of the courses that take place on Monday.
  3. The user clicks on a returned course to visit that course's detail page.
  4. They then click on the browser's back button to get back to the filtered view of courses.

In my case, this last step triggers the error - rather than navigating back to the previous url (including the query string), the page does not update and I see the following error in my console:

Screen Shot 2022-05-16 at 2 24 13 PM

I believe this is related to the use of history.replaceState to modify the url before the user navigates; it seems that when I programmatically update the current history entry with that function, Barba doesn't receive that change which then invalidates its own internal history state.

I was able to fix my issue by manually pushing a new history item to Barba's internal history state when I update the browser's native history entry:

history.replaceState({}, '', `${base}?${queryString}`);

// Adding this line fixed my issue.
barba.history.add(`${base}?${queryString}`, 'barba');

@saminton
Copy link

Any news on this ? I just ran into this issue also. It's been over 2 years since the initial bug report 🫠

As @braican stated it seems to be related to the use of history.replaceState(). history.pushState() however works fine. The solution using barba.history.add(); he mentioned isn't working for me I still get an error, also I do not want to add to the history state, juste change the url to reflect the choices on the current page.

This is all it takes to produce the error.

const url = new URL(window.location.href);
url.searchParams.set("category", category);	
history.replaceState({}, "", url);

The back button works but as soon as I go forward to an url that was modified I get this error

Screenshot 2023-04-20 at 12 30 07

Using @barba/core 2.9.7

@xavierfoucrier xavierfoucrier added this to the barba@next milestone Apr 20, 2023
@xavierfoucrier
Copy link
Member

Added to the next milestone, will work on it soon 🕶️

@xavierfoucrier xavierfoucrier changed the title Back forward button does not work correctly with query string Back forward button does not trigger transition correctly with query string Jul 26, 2023
@xavierfoucrier xavierfoucrier changed the title Back forward button does not trigger transition correctly with query string Popstate events doesn't trigger transition correctly with query string Jul 26, 2023
@xavierfoucrier xavierfoucrier mentioned this issue Jul 21, 2023
15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants