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

Clear outlet content (and update related internal data) at start of handling a navigation event. #884

Open
void-spark opened this issue Jul 8, 2024 · 0 comments

Comments

@void-spark
Copy link

Hi!

Is this project still maintained, if not any suggestions for other good standalone routers? :)

We have this request:
When a user triggers a navigation (e.g. clicking a menu link which updates the url).
A new element to show in the outlet is determined. It could be that for this decision, first some rest calls must be done which can take time. So the action returns a promise which might take some time to resolve.
When this happens, we would like to immediately remove the old element being held in the outlet, to be replaced later with the new element when the action resolves.
This would be visually clearer for the user in our case, to show navigation is in progress, but also helps with some technical issues (during the decision making, old data gets cleared, which means the old element misses data and starts giving errors).
Or instead of removing the old element, replacing it with a temporary stand-in would work too (actually, that could be nicer, then you can show please wait or so :) ).

I tried doing this by removing the element directly, without using the router. But the router holds some internal state, and if the old and new element are the same (according to the internal state), the new element isn't put back, so we end up with an empty page :)

What does work is hacking it into the router, with a method that clears both the element and internal state:
this.router = new class extends Router {
constructor() {
super(shadowRoot.querySelector('main'), {baseUrl: routerBaseUrl});
}

  clearCurrent() {
    const outlet = this.getOutlet();
    if (outlet?.firstChild) {
      outlet.removeChild(outlet.firstChild);
    }
    this.__previousContext = undefined;
  }

This seems to work, but is ugly and fragile :)

If there's already a better way of doing this, please let us know! :)

Thank you for you attention! :)

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

No branches or pull requests

1 participant