Repository navigation
fix: don't re-observe a detached element after destroy() - #247
Open
AndreasOhlsson wants to merge 1 commit into
Open
AndreasOhlsson wants to merge 1 commit into
AndreasOhlsson wants to merge 1 commit into
Conversation
|
@AndreasOhlsson is attempting to deploy a commit to the Formkit Team on Vercel. A member of the Team first needs to authorize it. |
A position update queued with lowPriority() before destroy() can run after it. updatePos() then calls observePosition() on the detached element and creates a new IntersectionObserver rooted at document.documentElement, which keeps the detached subtree alive for the rest of the session. Fixes formkit#246
AndreasOhlsson
force-pushed
the
fix/observe-detached-element
branch
from
October 8, 2026 10:35
6d40a49 to
c029f01
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #246.
What happens
A position update queued with
lowPriority()(requestIdleCallback) beforedestroy()can still run after it, for example when the component unmounts during a busy route change.updatePos(el)then callsobservePosition(el), which creates a newIntersectionObserverrooted atdocument.documentElementon the now-detached element. Nothing disconnects that observer, and it keeps the detached subtree alive for the rest of the session.The fix
observePosition()returns early for an element that is no longer in the document, after disconnecting any old observer. Every path that (re)creates an observer goes through this function, so one guard covers all of them:updateAllPosfinishedhandlersObserving a disconnected element measures nothing, so no animation behaviour changes for connected elements.
Evidence
destroy()and ran after it.destroy().useAutoAnimateon a timeline that remounts on navigation), over 60 navigations:Relation to #242
#242 (which fixes #180) tracks the poll start timers and stops polling in cleanup. That closes the timer-side leaks, but an idle callback that is already queued still reaches
observePosition()afterdestroy(). The two changes complement each other and don't conflict.I didn't add a test. The race needs a busy main thread around unmount, which the existing coarse
memory.spec.tsdoesn't cover. Happy to add a targeted e2e test modelled on it if you'd like one.Diagnosed with heap snapshots and a Playwright repro.