Skip to content

Clarify end-of-session event guidance for sendBeacon() - #45081

Open
nrps9909 wants to merge 1 commit into
mdn:mainfrom
nrps9909:codex/remove-sendbeacon-pagehide-fallback
Open

Clarify end-of-session event guidance for sendBeacon()#45081
nrps9909 wants to merge 1 commit into
mdn:mainfrom
nrps9909:codex/remove-sendbeacon-pagehide-fallback

Conversation

@nrps9909

@nrps9909 nrps9909 commented Aug 11, 2026

Copy link
Copy Markdown

Description

  • removes the obsolete "Use pagehide as a fallback" subsection
  • explains that visibilitychange can also fire when the user switches browser tabs
  • clarifies that pagehide is compatible with the bfcache but remains unreliable, especially on mobile

Motivation

All modern browsers implement visibilitychange, so recommending pagehide as a compatibility fallback is no longer necessary. The expanded guidance makes both the visibilitychange caveat and the remaining pagehide limitation explicit.

Validation

The focused file passes the repository pre-commit hook, URL and changed-xref checks, front matter validation, Prettier, Markdownlint, cSpell, and git diff checking.

Related issues and pull requests

Fixes #44927

@nrps9909
nrps9909 requested a review from a team as a code owner August 11, 2026 06:02
@nrps9909
nrps9909 requested review from sideshowbarker and removed request for a team August 11, 2026 06:02
@github-actions github-actions Bot added Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed labels Aug 11, 2026
@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Preview URLs (1 page)

(comment last updated: 2026-08-12 18:22:28)

@wbamberg

Copy link
Copy Markdown
Collaborator

Reading this back I think we also ought to expand a bit on pagehide in the previous section - with the following section removed, we are left with just one mention of pagehide, which appears kind of hanging. We could perhaps rename this section to something like "Legacy events", and add a paragraph at the end saying something like:

The pagehide event is not as problematic as unload or beforeunload, because it is compatible with the bfcache. However, it suffers from the same reliability problems as the other two events, so we recommend that developers should avoid it as well, and use only visibilitychange.

I wonder if we also ought to acknowledge the problem with visibilitychange: that it will fire in situations that are not what we might think of as the session end, such as the user switching to a different tab.

I'd also be happy if @philipwalton would like to confirm that this is still the best guidance for developers.

Remove the obsolete pagehide fallback and explain both its reliability limits and the broader visibilitychange trigger.

Signed-off-by: 陳廷安 <73953029+nrps9909@users.noreply.github.com>
@nrps9909
nrps9909 force-pushed the codex/remove-sendbeacon-pagehide-fallback branch from df4ac26 to 005c35b Compare August 12, 2026 14:49
@nrps9909 nrps9909 changed the title Remove obsolete pagehide fallback from sendBeacon() Clarify end-of-session event guidance for sendBeacon() Aug 12, 2026
@nrps9909

Copy link
Copy Markdown
Author

Thanks, this makes the guidance clearer. I updated the previous section to note that visibilitychange also fires when the user switches tabs, and added an explicit explanation that pagehide is bfcache-compatible but still unreliable for end-of-session analytics. I kept the existing heading so the published #avoid_unload_and_beforeunload fragment does not break. The branch is rebased and the focused pre-commit, URL/xref, formatting, Markdown lint, front matter, and spelling checks pass.

@wbamberg

Copy link
Copy Markdown
Collaborator

I think my version of the last paragraph here is better.

@Josh-Cena
Josh-Cena requested review from wbamberg and removed request for sideshowbarker August 14, 2026 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is 'Use pagehide as a fallback' clause still needed?

3 participants