Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 3 min read

Why preloaded videos can restart when you scroll in React

A video feed has an awkward timing problem: you want the next clip to load before someone reaches it, but you only want the current clip to play. In Vhoo, the AI image and video app I’m building, the mobile template fee

A video feed has an awkward timing problem: you want the next clip to load before someone reaches it, but you only want the current clip to play.

In Vhoo, the AI image and video app I’m building, the mobile template feed handles this with two separate values: retainSource and active.

That distinction matters because a React effect can accidentally reset a video at exactly the moment you want to use its preloaded data.

Imagine three cards:

Card Keep its source attached? Eligible for playback?
Previous Yes No
Current Yes Yes
Next Yes No

Cards farther away have their video source removed.

The feed calculates those values like this:

active={index === activeIndex}
retainSource={Math.abs(index - activeIndex) <= 1}

Now consider what happens when the user moves from card A to card B.

Card B already has its source attached. It becomes active, but retainSource remains true. Ideally, the player simply starts using the media it has been loading.

This is where an effect’s dependencies can cause trouble.

Suppose one effect handles both assigning the video source and starting playback. It depends on src, retainSource, and active. Its cleanup pauses the video and removes the source.

When active changes, React runs the old cleanup before running the effect again. That is the normal effect lifecycle.

The resulting sequence is:

  1. Card B preloads while it is next in the feed.
  2. The user scrolls to B.
  3. active changes.
  4. The effect cleans up B’s existing media source.
  5. The effect attaches the same source and loads it again.

The player has reset just as it becomes visible.

Calling video.load() resets the media element and starts source selection again. It can also abort pending playback operations. It is a meaningful lifecycle operation, as the media API documentation explains.

The browser may reuse cached bytes, so this does not necessarily mean downloading the entire file again. But the application has still restarted the player’s loading process unnecessarily.

The fix is to give source retention its own effect.

Here is the source-management portion, using a player ref attached to the video element:

useEffect(() => {
  const video = player.current;

  if (!video || !retainSource || !src) return;

  video.src = src;
  video.load();

  return () => {
    video.pause();
    video.removeAttribute('src');
    video.load();
  };
}, [retainSource, src]);

This effect has no playback decision to make, so it does not read active.

It runs when the URL changes, when the card enters or leaves the retained range, and during mounting or cleanup. Promoting the next card to the current card leaves its source alone.

Playback belongs in a separate effect. That effect responds to active, pauses when the card becomes inactive, and handles whether playback is currently allowed.

In our feed, that includes a hidden browser tab, the user’s reduced-motion preference, and a manual pause. Returning to the tab should not restart a video the user deliberately paused.

There is also an asynchronous detail: video.play() returns a promise. Playback can be rejected, including when browser policy blocks autoplay. The interface should reflect the result instead of assuming that calling the method means the video is playing. MDN’s play() reference describes these cases.

If a user scrolls away while a play request is pending, an old result must not update the newly active card’s state. Our hook uses a request counter to ignore outdated results, and its playing handler checks that the card is still active.

Source retention and playback now have different cleanup behavior:

  • Leaving the active position pauses the video.
  • Leaving the retained range removes its source.
  • Becoming active again while still retained does not call load().

Stable React keys matter here too. The feed uses each item’s ID as its key. Remounting the video element when its active state changes would defeat the separation.

Keeping one neighbor on each side is a deliberate tradeoff. It gives likely next clips a chance to load, but it may fetch video that the user never watches. Longer clips or tighter data budgets may call for a smaller retained range.

And preload="auto" remains a browser hint, not a promise that the next clip is fully buffered. The preload documentation makes that distinction explicit.

To check this behavior in your own feed, follow one card as it moves from next, to current, to previous. Watch calls to load() and changes to its source. Becoming current should trigger a playback decision without resetting the source.

That is the useful boundary: keep the media resource alive for as long as the retention policy allows, and let playback change independently as the user scrolls.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.