Skip to content

fix(react): recover from StoreError: DESTROYED on React <Activity> hide/reveal#1587

Merged
mihar-22 merged 2 commits into
videojs:mainfrom
ronald-urbina:fix/react/activity-store-destroyed
Jun 30, 2026
Merged

fix(react): recover from StoreError: DESTROYED on React <Activity> hide/reveal#1587
mihar-22 merged 2 commits into
videojs:mainfrom
ronald-urbina:fix/react/activity-store-destroyed

Conversation

@ronald-urbina

@ronald-urbina ronald-urbina commented May 22, 2026

Copy link
Copy Markdown
Collaborator

resolves #1583

Problem
When Player.Provider is used inside a React subtree (e.g. Next.js cacheComponents), navigating away and back throws StoreError: DESTROYED.

Why it happens:
useDestroy defers store.destroy() to a setTimeout(0) so React StrictMode's synchronous cleanup+remount can cancel it. That works because StrictMode's cycle is synchronous. With , the gap between cleanup and reveal is asynchronous — the timeout fires before React gets a chance to re-run effects, leaving the store destroyed. When Activity reveals the subtree, useState returns the same (now-destroyed) store reference and store.attach() throws.

Fix
Added a destroyed-store guard in the attach effect in create-player.tsx. If store.destroyed is true when the effect runs, a fresh store is created via setStore and the effect returns early. The resulting re-render re-runs the effect with a live store and attaches normally.

if (store.destroyed) {
setStore(createStore()(combine(...config.features)));
return;
}
StrictMode is unaffected — the timeout is cancelled before it fires, so store.destroyed is always false during StrictMode's re-mount.

Tests
Two tests added to the Provider describe block:

Activity recovery — destroys the store mid-flight, triggers the attach effect via setMedia, asserts no throw and a fresh store instance
StrictMode invariant — flushes fake timers after a StrictMode render, asserts the same store instance and that the deferred destroy was cancelled


Note

Medium Risk
Touches core player lifecycle and store recreation on attach; behavior is narrow (destroyed-store guard) but wrong timing could leak stores or double-attach in edge cases.

Overview
Fixes StoreError: DESTROYED when Player.Provider is hidden and shown again via React <Activity> (e.g. Next.js cache components). Deferred useDestroy cleanup can run in the async gap before effects re-run, so the same useState store is still destroyed when store.attach() runs.

The attach useEffect now checks store.destroyed when media is present: it **setStore**s a new combined store and bails once so the next run attaches with a live instance. StrictMode behavior is unchanged because the pending destroy is still cancelled on the synchronous remount.

Adds Provider tests for Activity-style destroy + setMedia recovery (new store, no throw) and for StrictMode (single store after flushing timers).

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

@netlify

netlify Bot commented May 22, 2026

Copy link
Copy Markdown

‼️ Deploy request for vjs10-site rejected.

Name Link
🔨 Latest commit a746227

@vercel

vercel Bot commented May 22, 2026

Copy link
Copy Markdown

@ronald-urbina is attempting to deploy a commit to the Mux Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented May 25, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
v10-sandbox Ready Ready Preview, Comment May 25, 2026 1:37pm

Request Review

@mihar-22 mihar-22 merged commit 058fb8c into videojs:main Jun 30, 2026
23 of 25 checks passed
@luwes luwes mentioned this pull request Jun 30, 2026
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.

Bug: StoreError: DESTROYED When Player.Provider Is Hidden and Re-Shown Inside React <Activity> (e.g. Next.js cacheComponents Route Transition)

4 participants