Skip to content

Bug: Intermittent "Cannot read properties of undefined (reading 'performance')" from mux-embed on programmatic mux-player creation #1340

Description

@kwiser

Is there an existing issue for this?

  • I have searched the existing issues

Which Mux Elements/Packages does this apply to? Select all that apply

mux-player

Which browsers are you using?

Chrome

Which operating systems are you using?

None

Description

We load @mux/mux-player dynamically (via injected <script>) and create the element programmatically with document.createElement. On some page loads - intermittently - initialization throws inside Mux Data (mux-embed) and the mux-player custom element never registers, leaving the player broken. On other loads of the exact same page it initializes fine and works normally, which points to a race condition rather than an environment problem.

Error:

Uncaught TypeError: Cannot read properties of undefined (reading 'performance')
at now (mux-embed@5.18.1 / @mux/mux-player)
at (@mux/mux-player)

The throwing code is mux-embed's timing helper, which reads .performance off the result of its internal "get global" function; at the moment now() is first called during init, that result is undefined.

Versions:

  • @mux/mux-player: loaded from https://cdn.jsdelivr.net/npm/@mux/mux-player (unpinned, resolves to current 3.x)
    • mux-embed: 5.18.1 (current latest)
      • Environment: WordPress host page, ~98 concurrent scripts, jQuery + core-js present. Reproduces in current Chrome.
        How we load it (approximate):

const el = document.createElement('mux-player');
el.setAttribute('playback-id', '...');
// ...attributes set...
// mux-player script injected dynamically on demand:
const s = document.createElement('script');
s.src = 'https://cdn.jsdelivr.net/npm/@mux/mux-player';
s.defer = true;
document.head.appendChild(s);
// element appended around the same time

What we've ruled out: the page environment is healthy on both success and failure loads - native, callable performance.now(); window, self, globalThis, top, parent all resolve to the window; not in an iframe; no CSP blocking the bundle (script loads 200); customElements.define works. Setting window.global = window before load reliably suppresses the throw, which suggests the global-resolution path in mux-embed's timing helper is the sensitive point.

Possibly related: this looks like the same family of Mux Data init-timing races as #1326 / #1199 (programmatic element creation plus async gap before DOM connection), though those describe a wrong video_id beacon rather than this performance crash. Does the fix in 3.13.1 cover this crash as well, or is a separate guard needed in mux-embed's now() / global resolution?

Reduced test case

No response

Steps to reproduce

Load a page that dynamically injects @mux/mux-player via a script tag (unpinned CDN URL) and creates a mux-player element with document.createElement, setting attributes before/around the time the script is appended.
Reload the page repeatedly (cold cache loads).
Observe the browser console on each load.

Current Behavior

Intermittently, the console shows: Uncaught TypeError: Cannot read properties of undefined (reading 'performance') thrown from mux-embed's now() helper. When this happens, customElements.get('mux-player') returns undefined - the element never registers and the player is completely broken on that page load. Other loads of the identical page succeed with no error and the player works normally.

Expected Behavior

The mux-player custom element should register reliably on every page load, regardless of timing/cold-cache conditions, so the player always initializes without throwing.

Errors

Uncaught TypeError: Cannot read properties of undefined (reading 'performance')
at now (mux-embed@5.18.1, bundled in @mux/mux-player)
at (@mux/mux-player)
at (@mux/mux-player)

What version of the package are you using?

Unpinned (loaded from https://cdn.jsdelivr.net/npm/@mux/mux-player, resolves to latest 3.x; mux-embed 5.18.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions