Clean up beacon on-visibility-change handling

The previous version was not posting the beacon when the page was
loaded and visible.
This commit is contained in:
Mike Dalessio
2025-08-09 14:09:34 -04:00
parent 323bb3cb68
commit c78bb652d5
2 changed files with 5 additions and 4 deletions
@@ -4,12 +4,15 @@ import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static values = { url: String }
visibilityChanged() {
connect() {
this.#sendBeacon()
this.onVisibilityChange = this.#sendBeacon.bind(this);
document.addEventListener("visibilitychange", this.onVisibilityChange)
}
disconnect() {
this.#sendBeacon()
document.removeEventListener("visibilitychange", this.onVisibilityChange)
}
#sendBeacon() {