Repository navigation
fix: re-initialize CldVideoPlayer after unmount and remount - #669
Merged
Merged
Conversation
The player was never fully disposed on unmount (only the Cloudinary plugin was), so it stayed registered with video.js. With an `id` prop, remounting reused that stale player and the new <video> element was never initialized; without one, every remount leaked a player. Next.js also doesn't fire onLoad again for a script id it already loaded. Dispose the player on unmount (skipping React Strict Mode's simulated unmount, which reuses the same element), add onReady alongside onLoad so remounts initialize again, and guard initialization with the player ref instead of possibly stale state.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
CldVideoPlayerdoesn't come back after it's unmounted and mounted again (for example after navigating away and back, or toggling it in the tree). The<video>element returns, but the Cloudinary player never re-attaches, so no controls and no poster. This is the remaining part of #572, which #602 attempted to fix.Root cause (two parts):
videojs.cloudinary.dispose(), which only disposes the Cloudinary plugin, so the player stayed registered with video.js.idprop, remounting found that stale player under the same id, and the new<video>element was never initialized.id, a new id is generated on each mount, so it appeared to work, but every remount left another live player behind.<Script>doesn't fireonLoadagain for a script id it has already loaded, so the player setup never ran.Changes (one file):
dispose()on unmount, which also removes it from the video.js registry. This is skipped when the player's<video>element is still in the page, which is the case during React Strict Mode's simulated unmount (the same element is reused anddispose()would remove it).onReadyalongsideonLoadon the player script.onReadyalso runs on later mounts once the script is loaded.playerInitializedstate, sinceonLoadandonReadycan both run during the same render and the state value there may be stale.The upload widget isn't affected: I checked that
CldUploadWidgetstill opens after a remount.Issue Ticket Number
Related to #572. Flagged during the v7.0.0 planning in #523.
Type of change
Checklist
Testing
Tested in the Next.js 16 test app with a page that removes and re-adds the player, in both a production build and dev mode (React Strict Mode), with and without an
idprop, over 4 mount cycles:ididCldVideoPlayerscenarios from my feat: upgrade Cloudinary video player from v1 to v4 #665 testing (one player each, no duplicates in Strict Mode, no page errors).onPlayandonPausestill fire once.