diff --git a/app/javascript/controllers/beacon_controller.js b/app/javascript/controllers/beacon_controller.js index 794565f78..2686f5d14 100644 --- a/app/javascript/controllers/beacon_controller.js +++ b/app/javascript/controllers/beacon_controller.js @@ -4,11 +4,17 @@ import { Controller } from "@hotwired/stimulus" export default class extends Controller { static values = { url: String } - connect() { - post(this.urlValue, { responseKind: "turbo-stream" }) + visibilityChanged() { + this.#sendBeacon() } disconnect() { - post(this.urlValue, { responseKind: "turbo-stream" }) + this.#sendBeacon() + } + + #sendBeacon() { + if (!document.hidden) { + post(this.urlValue, { responseKind: "turbo-stream" }) + } } } diff --git a/app/views/cards/show.html.erb b/app/views/cards/show.html.erb index f2c74b1cb..d4e26b686 100644 --- a/app/views/cards/show.html.erb +++ b/app/views/cards/show.html.erb @@ -42,7 +42,9 @@ <% end %> -