Skip to content

Prevent component destruction to support bfcache#476

Open
nikrom17 wants to merge 3 commits intomainfrom
feature/bfcache
Open

Prevent component destruction to support bfcache#476
nikrom17 wants to merge 3 commits intomainfrom
feature/bfcache

Conversation

@nikrom17
Copy link
Member

Summary

Enable bfcache (Back/Forward Cache) support by preventing component destruction when the page is entering bfcache.

Browsers fire pagehide with event.persisted = true when a page enters bfcache, and pageshow with event.persisted = true when restored. Previously, zoid treated every pagehide as a real navigation and
destroyed components — making them non-functional on bfcache restore.

Changes

Parent (src/parent/parent.js):

  • Remove once() wrapper on the pagehide listener so it can fire on every bfcache cycle
  • Add event.persisted guard — skip destroy() when the page is entering bfcache, only destroy on real navigation
  • Add logging to watchElementForClose callback to trace rerender vs close paths

Child (src/child/child.js):

  • Add event.persisted guard on the pagehide listener — skip checkClose signal when the page is entering bfcache

Component (src/component/component.js):

  • Add logging to the rerender() path for debugging bfcache restore behavior

How it works

Event persisted Behavior
pagehide true Page entering bfcache — skip destroy/checkClose, components survive
pagehide false/undefined Real navigation — destroy components as before
pageshow true Page restored from bfcache — components are still alive

Related PRs

Test plan

  • Load page with zoid components → navigate away → browser back button → components survive without re-render
  • Real navigation (close tab, navigate to external URL) → components destroy normally
  • Existing test suite passes (test/tests/error.js dispatches new Event("pagehide") with persisted=undefined, which is falsy — guard does not skip it)

@nikrom17 nikrom17 requested a review from a team as a code owner February 20, 2026 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant