I am not sure how to find out inside of a view that the browser has been refreshed/reloaded. Any ideas? Thanks
I’m interested in what are you doing with that information - what are you trying to achieve? Maybe there’s a standard way for it.
I have a login dialog, in case the login is not successful I want to keep the failure information on the screen while doing a redirect to the start page. In case of a browser refresh I want to clear this failure information. But I cannot make the difference between a redirect and a browser refresh.
I don’t think the browser makes any distinction between the cases. What you might be able to is to manually keep track of the previously rendered page and if the same one is rendered again without anything else in between, then you assume it’s a reload. This is still not a perfect mechanism since it will fail if the users opens the same URL in another tab or if they are interacting with the page in another tab before reloading.
You can get the browser window name. It is unique per tab. When opening a new UI you could check for that (async):
ui.getPage().retrieveExtendedClientDetails(receiver -> { receiver.getWindowName(); });
Instead of using the window name, I would store the failure information in a bean in the session scope and delete it once it has been read.
The issue is that I want to display the failure information only at a redirect and to clear it on a refresh
Can you tell me how do you redirect?
@Override
public void beforeEnter(BeforeEnterEvent event)
{ ...
try
{
....
}
catch (SecurityException e)
{
event.rerouteTo(LoginScreen.class);
}
}
In the LoginScreen’s BeforeEnterEvent use event.getRerouteUrl()
to determine if it was rerouted. Returns null when it’s not.
I always get this : 2025-01-21 11:42:47,782|System |ERROR|java.lang.NullPointerException: Cannot invoke "com.vaadin.flow.router.NavigationState.getResolvedPath()" because "this.rerouteTargetState" is null 2025-01-21 11:42:47,783|System |ERROR| at com.vaadin.flow.router.BeforeEvent.getRerouteUrl(BeforeEvent.java:1015)
Jup that does not work. NPE is normal here.
Check out event.getTrigger()
. It is PROGRAMMATIC when the view was rerouted/forwarded and PAGE_LOAD when reloaded.
But when using reroute the URL stays the same, which means when reloading the page, it would trigger a reroute again. You would need to use forwardTo instead of rerouteTo on the SecurityException.
Also I came across event.rerouteToError. May this could be a solution too.
Docs: https://vaadin.com/docs/latest/flow/routing/exceptions