brisa-build / brisa

The Web Platform Framework.
https://brisa.build
MIT License
449 stars 12 forks source link

onMount won't trigger again when doing SPA #581

Closed AlbertSabate closed 1 month ago

AlbertSabate commented 1 month ago

Describe the bug When loading a web component with defined onMount, then doing SPA navigation to another page, and come back to the previous page, onMount won't get triggered again.

The bug seems because the component never gets unmounted after changing page?

To Reproduce Pending to add a reproduction @AlbertSabate (Will do this week) and potentially more information about it.

Expected behavior onMount should be triggered every time the component is mounted. Component should be properly unmounted if you navigate using SPA to a page where this component does not exists.

aralroca commented 1 month ago

If the web component exists in both pages, it is not dismounted and mounted again, it is the grace to maintain the state of it without being dismounted. This is implemented this way on purpose. This way the layout remains the same after browsing. If you need to dismount and mount it during navigation, you can use a key based on the route. When the key change force the unmount/mounting. But by default, it's not dismounted on purpose.

aralroca commented 1 month ago

In the case of a page without the same Web Component should unmount and mount again always.

AlbertSabate commented 1 month ago

@aralroca, this is yet unclear where the issue comes from. After more debug it does not seem to come from effects or unMount. It is for that I wrote a note "Pending to add a reproduction @AlbertSabate". Something is off with some code I'm currently debugging and trying to narrow it down. I will use this issue for me to track down tests I did and not to forget. Please ignore it until I can clarify the root issue.

AlbertSabate commented 1 month ago

From this code:

effect(() => {
    console.log(
      'Im here',
      self.hasAttribute('tabindex'),
      self,
    );
    if (!self.hasAttribute('tabindex')) {
      self.setAttribute('tabindex', '0');
    }
  });

When loading the component for first time and second time seems something different.

Screenshot 2024-10-23 at 4 46 38 PM

@aralroca why the first time is an object and second an HTMLElement?

AlbertSabate commented 1 month ago

Interestingly, the first time appends properly the attr tabindex, but the second time no.

AlbertSabate commented 1 month ago

Confirmed unMount and effects it is working as expected.

aralroca commented 1 month ago

From this code:

effect(() => {
    console.log(
      'Im here',
      self.hasAttribute('tabindex'),
      self,
    );
    if (!self.hasAttribute('tabindex')) {
      self.setAttribute('tabindex', '0');
    }
  });

When loading the component for first time and second time seems something different. Screenshot 2024-10-23 at 4 46 38 PM

@aralroca why the first time is an object and second an HTMLElement?

An object? Well, elements are objects, I guess it's something in DevTools that doesn't show it well, but you can use it as an element without problems, right?

AlbertSabate commented 1 month ago

I can use it, and it seems properly selecting the DOM, however, it behaves differently the first time appends the setAttribute correctly. The second time does not work. I'm investigating why.

AlbertSabate commented 1 month ago

Verified Brisa is working perfect on version 0.1.3.canary.4