Javascript resource management

Hello everyone,

When navigating in our application, we need to load javascript resources, that’s the ok part.
The problem is: when navigating in other pages, javascript loaded resources remains and generates errors because needed dom is not written anymore.

Is there a way to load javascript only for current page then discard resources?
UI.getCurrent().getPage().loadJavascript does not discard javascript resource on navigation

Maybe someone will correct me if I’m wrong, but if I understand correctly, you can’t unload JavaScript resources you’ve loaded in a single-page app like Vaadin. Instead, you’ll need to modify the script so that its behavior can be disabled when a suitable view isn’t currently displayed. This disabling or enabling could happen in a navigation lifecycle event handler, like a BeforeEnterEvent observer: https://vaadin.com/docs/v14/flow/routing/tutorial-routing-lifecycle.html

Hello Olli,

Using BeforeLeaveEvent allows us to make a workaround on our problem.
Thank you for your help