Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): fall back to the raw upload when the playlist retry signs the same URL

Richie McIlroyab1bd927e088
diff --git a/apps/web/__tests__/unit/share-player-server-source.test.ts b/apps/web/__tests__/unit/share-player-server-source.test.ts
index 6fe27fb82..634688bf6 100644
--- a/apps/web/__tests__/unit/share-player-server-source.test.ts
+++ b/apps/web/__tests__/unit/share-player-server-source.test.ts
@@ -386,4 +386,37 @@ describe("share MP4 source rendered on the server", () => {
player.client.clear();
}
});
+ it("moves on to the raw fallback when the playlist retry returns the same URL", async () => {
+ const fetchImpl = vi
+ .fn<typeof fetch>()
+ .mockResolvedValueOnce(response(SERVER_URL, 206, true))
+ .mockResolvedValueOnce(
+ new Response(null, {
+ status: 206,
+ headers: { "content-type": "video/mp4" },
+ }),
+ );
+ vi.stubGlobal("fetch", fetchImpl);
+ const { props, videoRef } = playerProps({
+ rawFallbackSrc: "/api/playlist?videoType=raw-preview",
+ });
+ const player = await hydratePlayer(props);
+ try {
+ await act(async () => {
+ videoRef.current?.dispatchEvent(new Event("error"));
+ });
+ await settleFetch(fetchImpl, 2);
+ expect(fetchImpl.mock.calls[0]?.[0]).toMatch(PLAYLIST_PROBE);
+ expect(fetchImpl.mock.calls[1]?.[0]).toMatch(RAW_PROBE);
+ await vi.waitFor(() => {
+ expect(videoRef.current?.getAttribute("src")).toMatch(RAW_PROBE);
+ });
+ expect(player.container.textContent).not.toContain(
+ "Could not load a playable video source",
+ );
+ } finally {
+ await act(async () => player.root?.unmount());
+ player.client.clear();
+ }
+ });
});
diff --git a/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx b/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
index ccd451528..b7a32c0f5 100644
--- a/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
+++ b/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
@@ -515,6 +515,28 @@ export function CapVideoPlayer({
setVideoLoaded(false);
setHasError(false);
setShowPlayButton(false);
+ // Only a server-rendered source reaches here with a raw fallback. The
+ // playlist route can sign the same URL again within a second, so a
+ // retry that changes nothing moves on to the raw upload.
+ const stopRetrying = () => {
+ playbackToRestore.current = null;
+ setIsRefreshingInitialPlaybackSource(false);
+ if (
+ shouldFallbackToRawPlaybackSource(
+ "mp4",
+ rawFallbackSrc,
+ hasTriedRawFallback,
+ )
+ ) {
+ setHasTriedRawFallback(true);
+ setPreferredSource("raw");
+ setVideoLoaded(false);
+ setHasError(false);
+ setShowPlayButton(false);
+ return;
+ }
+ setHasError(true);
+ };
void resolvedSrc
.refetch()
.then((result) => {
@@ -522,16 +544,10 @@ export function CapVideoPlayer({
!result.data ||
result.data.url === playbackToRestore.current?.sourceUrl
) {
- playbackToRestore.current = null;
- setIsRefreshingInitialPlaybackSource(false);
- setHasError(true);
+ stopRetrying();
}
})
- .catch(() => {
- playbackToRestore.current = null;
- setIsRefreshingInitialPlaybackSource(false);
- setHasError(true);
- })
+ .catch(stopRetrying)
.finally(() => {
initialPlaybackSourceRetryPending.current = false;
});