-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Core Browser] Call stop
on beforeunload
event
#132696
Comments
Pinging @elastic/kibana-core (Team:Core) |
The only valid use case this event is supposed to be used for is user confirmation when leaving a page with unsaved changes, even moz's usage notes states it. Overall, there's no reliable way to make sure that some code will be executed in every scenario that could cause the page/tab/browser to terminate, which is why ihmo we shouldn't attach cleanup logic to it. The main risk I see by doing so is to have developer think their code will always be executed on page termination, when in fact, it's not. |
The
Also, writing to the localStorage is a synchronous API that should not take long to execute and could be used in this event, couldn't it? Probably, we can discuss an alternative event to hook into? The reason I think
FWIW, isn't that the same problem on the server? There are multiple use cases that might not call the
To be clear: the main use case I'm thinking about at the moment is EBT on the browser. After some thorough testing, I noticed that pages that navigate via a full page refresh fail to report the We could implement this listener in the EBT client itself if we don't want to make it a |
Not always. See the example from mzdn: On mobile, you open a page, you go back to the home screen via home button, your browser app hibernates, then you close your browser app from the system menu, no events. On some non-mobile browsers, this works too: switch tab, close your browser, the non-active tabs will not necessarily trigger
To my knowledge, this is the best event to hook into, but still a terrible one. That's the thing.
It totally is, and it's why I've been fighting against doing 'mandatory' cleanup operations during
I'm not against implementing it, and there probably isn't a better solution. My point is: we need to be aware that it is not a reliable way to execute something before a page reload (and that there is no reliable way) |
I get and share that sentiment. Do you think it's best to listen to the event (and handle potential cancellations somehow) in the package instead of relying on Core to call the |
Probably not... It's still probably better to have a single handler for the event, at least to handle cancelation as you said. |
I think it's worth taking a best-effort approach to calling
Looks like we actually have documented this exact scenario as a use case for the |
So, I got a PR ready(#187938)... However @jloleysens found out that the The thing is, those two events are quite different from what As I was saying to JL, this is ihmo yet another example of why trying to have the exact same architecture on our browser-side and server-side "root" systems is probably a mistake: if the concept of "stopping" is quite concrete on the server, it is way less on the browser (especially on modern browsers, even more on modern mobile browsers), as closing your browser app, or putting the app on sleep, is not the same as killing the tab. So all this to say: I think we do have a need for plugins to be able to "hook" into a "status change". One example is our telemetry that would want to be able to flush its events when the page's state changes. However, I don't think hooking into Instead, we should accept that in real life scenario (non test, real user, real browser), the concept of "stopping" browser-side Kibana is abstract, and should either:
WDYT? |
Yeah I like this idea. I thought using "unload" while we can would be OK, but totally agree there is a better solution. If the primary use case is to flush telemetry then unload's "defined" behaviour seems like it could work in some cases, but be flaky at best. Of your alternatives I like (1). Main benefit of (2) afaict is saving plugins/core 1 loc by exposing these events as observables which doesn't seem worth the lift imo. Perhaps we should update the issue title too? |
@afharo did you have any particular use case in mind when you opened the issue? |
@pgayvallet, yes, this one: https://github.com/elastic/kibana/blob/main/packages/core/analytics/core-analytics-browser-internal/src/analytics_service.ts#L79-L82 But I can imagine other use cases like aborting requests in Dashboard/Discover/Lens when closing the tab/window/leaving the page. |
Would this be doable from the HTTP service? And, does the browser not do this already? |
Yeah... probably... but we still need a trigger for the HTTP service (similar to the Analytics service force-flushing the pending events before closing). That trigger can be the I'd rather rely on
Maybe? I don't know. |
Yeah, me too, but that's the thing, it seems we can't... Which is why my question is mostly: Given the development cost will be way higher than initially anticipated, do we think we still need this in the short/mid term, or should we just postpone it for now. |
I think postponing is fine. It's quite possible that all events are shipped by the time there's an attempt to close the tab. And missing out on the edge cases is probably ok, bearing in mind the level of effort. Ultimately, if we really need to force-flush events, we can call the flush API on onbeforeunload from the service itself. I'll close this issue for now. |
Some plugins/services in Kibana may need to apply some persisting logic (to local storage or an unattended HTTP request) when the user leaves Kibana.
By definition, this piece of logic should be implemented in the
stop
method. However, we are only calling it when there's a Fatal Error:kibana/src/core/public/core_system.ts
Lines 112 to 115 in 8539a91
We have some piece of logic handling the
beforeunload
event, but it only calls theleaveHandler
of the current application:kibana/src/core/public/application/application_service.tsx
Lines 402 to 413 in 9d5aca5
This means UI services cannot properly handle their unload logic.
Should we call
stop
on this event if theleaveHandler
is not a "confirm" action?The text was updated successfully, but these errors were encountered: