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

Add failing test for x-for after back with wire:navigate #7034

Conversation

gehrisandro
Copy link
Contributor

This PR adds a failing test for x-for after the back button is used with wire:navigate.

Given the following code:

<div x-data="{ items: ['item 1', 'item 2', 'item 3'] }">
    <template x-for="item in items" :key="item">
        <div class='item' x-text="item"></div>
    </template>

    <a href="/second" wire:navigate dusk="link">Go to second page</a>
</div>

After navigating to the second page and returning back by using the browsers "back" button. There are six .item divs in the DOM and there are three warnings and three errors in the console.

Screenshot 2023-10-10 at 15 01 27

@gehrisandro
Copy link
Contributor Author

gehrisandro commented Oct 10, 2023

I was able to trace this down a little.

It looks like when x-for updates, it creates all elements based on an index and tries to find existing items based on a value stored on the template element in the attribute _x_prevKeys.

When using wire:navigate only the raw HTML gets stored to the session storage. Therefore _x_prevKeys is empty when restoring the DOM from the session storage.

If I do manually remove the rendered div elements from the session storage before hitting the back button everything seems to work fine. So I think there are two options how to proceed:

  1. We need to store and restore the value of _x_prevKeys before saving the HTML to the session storage.
  2. We could use the cleanup() function on the x-for directive before storing the HTML.

As I don't know if I am on the right track I would like to hear your opinions before proceeding. @calebporzio

Update: Looks like there is a workaround for this: #6849 (reply in thread)
But I think we should fix this properly. 😅

@robsontenorio
Copy link
Contributor

Following along, just ran into this issue

@robsontenorio
Copy link
Contributor

Did you guys found any workaround?

@mybouhssina
Copy link

@robsontenorio I add a class like 'to-remove-before-navigating' to every child in x-for (or any other element that cause this kind of problems), and add the following script:

 document.addEventListener('livewire:navigating', () => {
    document.querySelectorAll('.to-remove-before-navigating').forEach(el => {
        el.remove();
    });
});

I'm pretty sure I've found this code on some other issue/discussion but I'm not sure where.

@robsontenorio
Copy link
Contributor

@mybouhssina I am using that hack... pretty annoying :(

@mybouhssina
Copy link

@robsontenorio yeah it's pretty annoying, but much better than the errors whenever I press the back button ^^'
Hope there will be a fix soon !

@calebporzio
Copy link
Collaborator

Good points. I think the answer here is doing this in the cleanup() of x-for. I would welcome a PR for that on Alpine!

@calebporzio calebporzio closed this Jan 4, 2024
@gehrisandro
Copy link
Contributor Author

Good points. I think the answer here is doing this in the cleanup() of x-for. I would welcome a PR for that on Alpine!

Okay. I can give it a try.

@robsontenorio
Copy link
Contributor

Hey @gehrisandro

Have you sent another PR to handle this ?

@danie-ramdhani
Copy link
Contributor

isn't it navigate plugin (x-navigate or wire:navigate) only available on livewire?
alpinejs/alpine#3720
shall we reopen this PR?

@danie-ramdhani
Copy link
Contributor

nvm this happened in alpine + htmx too (alpinejs/alpine#2924)

@gehrisandro
Copy link
Contributor Author

Hey @gehrisandro

Have you sent another PR to handle this ?

No, not yet. I am busy with other stuff but maybe I will come back to this one later.

@danie-ramdhani
Copy link
Contributor

fixed alpinejs/alpine#4015

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

Successfully merging this pull request may close these issues.

6 participants