Use pagehide and persisted to separate pausing from cleanup in browser pages
A page hidden during history navigation may be kept in the back/forward cache and restored later, so a blanket pagehide cleanup can wipe state the restored page needs. The recommended approach is to register a window-level pagehide listener and check event.persisted to decide between two paths. When persisted is true, work is paused while resumable state such as an unfinished draft is kept, and when it is false or absent, state is released through the cleanup path. The example code returns early on the persisted branch so cleanup cannot run afterward, and it returns an uninstaller function for components that may mount more than once. Tests with a window-like dependency can verify the application's branching but cannot confirm real bfcache eligibility, which the browser decides. The author notes that this pattern is meant for history-navigation lifecycle policy rather than universal shutdown handling.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in