Review the failure modes before accepting the handler
The risky implementation is not a missing event listener. It is a listener that takes the same cleanup action for every pagehide event.
Start with the failure assertions. On a persisted event, cleanup must not run and the unfinished draft must remain intact. If either condition fails, a browser history restoration can return to a page whose resumable state was destroyed. On a non-persisted event, the opposite must hold: work is not merely marked paused, cleanup runs, and the draft is removed. A third case passes a synthetic event without persisted and checks the defensive cleanup path.
Prerequisites
You need a browser-facing integration point that can register a pagehide listener, plus two application operations with distinct ownership rules: one that pauses resumable work and one that releases non-resumable state. Keep these operations separate before wiring lifecycle events to them.
The implementation decision
The handler should trust the event contract instead of guessing from navigation type or user-agent behavior. A true persisted value selects the suspension path: stop work that should not run while hidden, but retain state in case the page is restored from bfcache. Every other event selects final cleanup. Returning an unregister function avoids accumulating listeners when the surrounding integration mounts again.
What this proves—and what it does not
With an injected recording dependency, the success path proves the application branch: persisted means pause and retain; non-persisted means clean up and discard. It does not prove that a real browser will place a particular page in bfcache, because cache eligibility belongs to the browser and depends on the page and environment.
One limitation remains: this is not a universal page-termination mechanism. In particular, mobile lifecycle conditions may not reliably emit pagehide. Use the pattern for the ownership decision during history navigation, not as a substitute for every shutdown or analytics requirement.
Working example
/**
* Installs the pagehide branch used by a browser page.
*
* This unit-testable function proves only the application's branching behavior.
* A real browser must supply the PageTransitionEvent during navigation.
*/
export function installPagehideLifecycle(windowLike, work) {
if (!windowLike || typeof windowLike.addEventListener !== "function") {
throw new TypeError("windowLike must provide addEventListener");
}
const { pause, cleanup } = work ?? {};
if (typeof pause !== "function" || typeof cleanup !== "function") {
throw new TypeError("pause and cleanup must be functions");
}
function onPageHide(event) {
// A persisted page may be restored from the back/forward cache, so retain
// resumable state while stopping work that should not continue while hidden.
if (event?.persisted === true) {
pause.call(work);
return;
}
// Without persistence, the page will not be restored from this transition.
cleanup.call(work);
}
windowLike.addEventListener("pagehide", onPageHide);
return () => {
if (typeof windowLike.removeEventListener === "function") {
windowLike.removeEventListener("pagehide", onPageHide);
}
};
}
Read the full article: https://chriseugenerodriguez.com/blog/pagehide-persisted-cleanup