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

Improvement: com.vaadin.flow.router.BeforeLeaveObserver #8400

Open
netbeansuser2019 opened this issue May 21, 2020 · 4 comments
Open

Improvement: com.vaadin.flow.router.BeforeLeaveObserver #8400

netbeansuser2019 opened this issue May 21, 2020 · 4 comments

Comments

@netbeansuser2019
Copy link

if "route component" or even on layout implements com.vaadin.flow.router.BeforeLeaveObserver apply:

https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event

void beforeLeave(BeforeLeaveEvent event) {
if (event.isBeforeUnLoadBrowserEvent()) {
event.signalUnSavedData(); //to signal unsave data so browser dialog should be shown if possible
} else if (event.isUnloadBrowserEvent() && !event.isFromClient()) {
//housekeeping
} else {
//navigating out by Vaadin framework
}
}

@netbeansuser2019
Copy link
Author

So without that e.g. any opening pages from internet-browser-bookmarks cannot signal leaving of page.

@juhopiirainen
Copy link

@netbeansuser2019 thank you for the ticket. I move this to the Flow team for further investigations.

@juhopiirainen juhopiirainen transferred this issue from vaadin/platform May 21, 2020
@Legioth
Copy link
Member

Legioth commented May 22, 2020

Neither beforeunload nor unload are appropriate for this use.

  • beforeunload is fired spuriously in some situations which would lead to premature reactions. This happens e.g. when the user clicks a link that ends up starting a download instead of unloading the page.
  • unload happens "too late" and the only way of ensuring the request actually gets delivered to the server is by doing a synchronous XHR request which will block the user from actually navigating away until a response is received. Browsers have even started disallowing this behaviour: https://www.chromestatus.com/feature/4664843055398912

Instead, we would have to use either Beacon or fetch with keepalive. The limitation there is that either of those isn't supported in all relevant browsers. Another problem for some use cases is that those are fire and forget, but there isn't any way for the server to return instructions that would keep the page loaded.

@netbeansuser2019
Copy link
Author

netbeansuser2019 commented Jun 8, 2020

Dear Vaadin team,
even if it is not possible e.g. due to limit settled by internet browser for "unload" and so on you can do these minimal solutions:

  1. give javascript API to change location safely like:
    onclick="vaadin.setLocation(url)"
    so your javascript simply do:
  • recognize if it is relative (particular registered route or route alias) or full like "https..."

  • contact server to do navigation to such page

  • if there is beforeLeave event processing and if it is rejected such leave stay on page

  • if it is different page (including route aliases) do close ui to relase resources for
    garbage collector job

  • if navigation server request fail so show proper message, no that general one as now and offer
    to user a link without any contacting server (for route path include virtual root) as e.g. user could
    be disconnected and time-out or even he/she forgets about did logout on other tab or window
    before his/her "break" (e.g. lunch break) that needs extra login anywhere. So to have all the time
    a way to navigate to particular page even if it is fail then that access to such resource need any
    authentication (loggin in), but it is on implementors to handle this.

  • In no way use forwarding, but rather redirecting as that it is against to Spring Security as rules are
    omitted then (like it is done currently just by "navigation calls" initiated from server).

  1. Give e.g. annotation to set own "activity-interval" e.g. to release some "heavy inactive pages/ui" in
    short time
  2. add to XHR or framework calls extra parameter/header that will be excluded from address bar
    and/or history so as flag indicating that it is possible that user uses unexpected way of navigation
    so such UI can be released earlier.

Also in context of #8399

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

No branches or pull requests

4 participants