Skip to content

fix(scanner): always render video element to avoid race condition - #125

Merged
Gaubee merged 1 commit into
mainfrom
fix/scanner-video-race-condition
Dec 26, 2025
Merged

Gaubee merged 1 commit into
mainfrom
fix/scanner-video-race-condition

Conversation

@Gaubee

@Gaubee Gaubee commented Dec 26, 2025

Copy link
Copy Markdown
Contributor

Problem

Camera video doesn't render on GitHub Pages, but works locally.

Root Cause

Race condition: video element was conditionally rendered based on state:

{(state === 'scanning' || state === 'success') && (
  <video ref={videoRef} ... />
)}

But initCamera tried to set srcObject before state changed:

videoRef.current.srcObject = stream;  // videoRef.current is null!
setState('scanning');  // video renders AFTER this

Solution

Always render the video element, use CSS invisible class to hide it when not active. This ensures videoRef.current is available when initCamera runs.

The video element was conditionally rendered based on state, but
initCamera tried to set srcObject before state changed to 'scanning'.
This caused videoRef.current to be null in production.

Fix: Always render video, use CSS 'invisible' to hide it when not active.
@Gaubee
Gaubee merged commit abf9022 into main Dec 26, 2025
5 checks passed
@Gaubee
Gaubee deleted the fix/scanner-video-race-condition branch December 26, 2025 08:57
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