Skip to content

fix: defer load() until connected to prevent metadata race on first Mux Data beacon#1326

Open
R-Delfino95 wants to merge 3 commits intomuxinc:mainfrom
R-Delfino95:fix/metadata-race-condition
Open

fix: defer load() until connected to prevent metadata race on first Mux Data beacon#1326
R-Delfino95 wants to merge 3 commits intomuxinc:mainfrom
R-Delfino95:fix/metadata-race-condition

Conversation

@R-Delfino95
Copy link
Copy Markdown
Contributor

@R-Delfino95 R-Delfino95 commented Apr 29, 2026

Problem

Fixes #1199

When <mux-player> (or <mux-video>) is created programmatically and attributes are set before the element is appended to the DOM — with any async yield between attribute setting and appendChild (e.g. await Promise.resolve(), React's async reconciliation, Next.js rendering, etc.) — the first Mux Data analytics beacon is sent with an incorrect video_id.

Root cause: mux-video.#requestLoad() schedules load() in a microtask via await Promise.resolve(). Under the following sequence, that microtask fires before mux-player.connectedCallback has a chance to propagate metadata-* attributes to the inner <mux-video>:

const el = document.createElement('mux-player');
el.setAttribute('playback-id', '...');
el.setAttribute('metadata-video-id', 'my-custom-video-id'); // not in observedAttributes
await Promise.resolve(); // ← load() microtask fires here — metadata is empty
document.body.appendChild(el);  // connectedCallback propagates metadata too late

metadata-* attributes are not in mux-video's observedAttributes. They are read lazily from the element's own DOM attributes by the metadata getter. Since mux-player's template does not forward metadata-* attributes to the inner , propagation only happens in mux-player.connectedCallback. Any async gap before appendChild causes the first mux.monitor() call — which sets video_id permanently for the view — to receive empty metadata. Because video_id is fixed at view start and cannot be corrected retroactively, the beacon is permanently wrong.

Fix

Added an isConnected guard inside mux-video's #requestLoad()

When the element is not yet connected at microtask time, the deferred load() is skipped. The existing connectedCallback already re-triggers #requestLoad() once the element connects (guarded by !this.#core), so load() runs after mux-player.connectedCallback has propagated metadata


Note

Medium Risk
Changes initialization timing and reload behavior for mux-video, which can affect first-play analytics and some playback edge cases around connect/disconnect and disable-tracking toggles.

Overview
Fixes a race where MuxVideoBaseElement could call load() from the #requestLoad() microtask before the element is connected, causing the first Mux Data beacon to start with missing metadata-* values.

#requestLoad() now bails out when !isConnected, relying on connectedCallback() to re-trigger the load once attached. Relatedly, toggling disable-tracking no longer unloads/reloads while disconnected to avoid a .then() resume without a corresponding load().

Reviewed by Cursor Bugbot for commit 5ebec4a. Bugbot is set up for automated code reviews on this repo. Configure here.

@R-Delfino95 R-Delfino95 requested a review from a team as a code owner April 29, 2026 17:01
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 29, 2026

@R-Delfino95 is attempting to deploy a commit to the Mux Team on Vercel.

A member of the Team first needs to authorize it.

@snyk-io
Copy link
Copy Markdown

snyk-io Bot commented Apr 29, 2026

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit e4944fc. Configure here.

Comment thread packages/mux-video/src/base.ts
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.

Mux player WC metadata race condition

1 participant