hls-video-element: HLS instances are initialized on disconnected elements during React Suspense retries

Open
#269 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, react
Domain
frontend, web-dev

Research direction

Start by reading the load() implementation in the hls-video-element code and trace how custom element connection and destruction are handled. Reproduce the race with React Suspense or repeated load() calls, then verify that abandoned or disconnected elements do not create HLS instances while a valid load still starts playback.

Written by the indexing model from the issue text.

Description

When using hls-video-element with React Suspense, multiple HLS instances can be created for custom elements that never become connected to the DOM.This appears to be caused by asynchronous initialization in load().
The current implementation calls #destroy(), then awaits Promise.resolve() before creating a new HLS instance:

async load() {
  const isFirstLoad = !this.api;
  this.#destroy();
  if (!this.src) return;
  if (Hls.isSupported()) {
    await Promise.resolve();
    this.api = new Hls({
      liveDurationInfinity: true,
      autoStartLoad: false,
      ...this.config,
    });
    this.api.loadSource(this.src);
    this.api.attachMedia(this.nativeEl);
  }
}

During the asynchronous gap, the element can be disconnected from the DOM or another load() invocation can occur. There is currently no check to ensure that the element is still connected or that the pending load operation is still valid before creating the HLS instance.
As a result, an HLS instance may be created for an element that React has already abandoned.

example sandbox: https://codesandbox.io/p/sandbox/2zlv3c

or simply render lazy components together in one Suspense wrapper

import { Suspense, lazy, useEffect, useState } from 'react';
import HlsVideo from 'hls-video-element/react';

<Suspense fallback={<div>loading…</div>}>
    <HlsVideo src={SRC} preload="auto" muted style={{ width: 320 }} autoplay={false} />
    <LazySiblingA />
    <LazySiblingB />
    <LazySiblingC />
</Suspense>

in the network tab you can also see 5 request instances for one video:

Image

note: This race condition can occur without React Suspense. Suspense is just one way to expose it.

Dominant language
JavaScript
Stars
266
Forks
53
Avg merge
13m
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.