fix(react): use useLayoutEffect for hasMounted to prevent source element race condition#1751
Open
pyto86pri wants to merge 1 commit intovidstack:mainfrom
Open
Conversation
…ent race condition On iOS 17+, hls.js uses ManagedMediaSource which calls removeSourceChildren() to remove all <source> elements from the <video> element during initialization. When this runs before React's batched re-render (triggered by setHasMounted) commits, React tries to removeChild already-removed <source> elements, causing a NotFoundError. This race condition is especially likely during unmount→remount transitions (e.g. page navigation in Next.js), where React's scheduler may yield before processing the hasMounted re-render due to the additional cleanup work, allowing the RAF-scheduled hls.js initialization to fire first. Using useLayoutEffect ensures setHasMounted(true) triggers a synchronous re-render during the commit phase, removing the <source> elements before any RAF callbacks can fire. Closes vidstack#1521 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
useLayoutEffectinstead ofuseEffectforsetHasMounted(true)inMediaOutletto prevent a race condition with hls.js'sremoveSourceChildren()Problem
On iOS 17+, hls.js uses
ManagedMediaSourcewhich callsremoveSourceChildren()during initialization, removing all<source>elements from the<video>element — including ones rendered by React (when!hasMounted).The current
useEffectforsetHasMounted(true)triggers a batched re-render that may be deferred by React's scheduler (especially during unmount→remount transitions like Next.js page navigation). This allows the RAF-scheduled hls.js initialization to fire first, removing React's<source>elements before React can reconcile them, resulting in aNotFoundErrorfromremoveChild.Timeline (before fix)
Timeline (after fix)
Notes
useLayoutEffectdoes not run on the server, so<source>elements are still rendered in SSR HTML for preloadinguseLayoutEffectre-render is lightweight (only removes<source>children), so no visible paint delayCloses #1521