Skip to content

Production-ready animations, transitions, backgrounds, and scenes for Remotion

fix(dev): show a rebuilt preview config without a render loop

kapishdima5cb17809f44f
diff --git a/components/docs/blocks/player/player-stage.tsx b/components/docs/blocks/player/player-stage.tsx
index d50f309e..e7deee60 100644
--- a/components/docs/blocks/player/player-stage.tsx
+++ b/components/docs/blocks/player/player-stage.tsx
@@ -119,13 +119,20 @@ export function PlayerStage({
// The stage wears the composition's backdrop while its chunk loads, and the
// first frame fades in over it instead of cutting in from black.
const [loaded, setLoaded] = useState(false);
+ // By value: a config that is loaded again (a dev rebuild) brings an equal
+ // backdrop in a new object, and a new `lazyComponent` makes the Player load
+ // the composition from scratch.
+ const backdropKey = previewBackdrop ? JSON.stringify(previewBackdrop) : "";
const compProps = useMemo<
| { lazyComponent: () => Promise<{ default: AnyComposition }> }
| { component: AnyComposition }
>(() => {
+ const backdrop: BackdropFill | undefined = backdropKey
+ ? JSON.parse(backdropKey)
+ : undefined;
const wrap = (Inner: AnyComposition) => (p: Record<string, unknown>) =>
- previewBackdrop ? (
- <Backdrop fill={previewBackdrop} padding={0} radius={0} shadow="">
+ backdrop ? (
+ <Backdrop fill={backdrop} padding={0} radius={0} shadow="">
<Inner {...p} />
</Backdrop>
) : (
@@ -142,7 +149,7 @@ export function PlayerStage({
};
}
return { component: wrap(Component ?? (() => null)) };
- }, [Component, load, previewBackdrop]);
+ }, [Component, load, backdropKey]);
const poster =
previewBackdrop && previewBackdrop.type !== "image"
diff --git a/lib/previews/index.ts b/lib/previews/index.ts
index a3969cd1..4212ea47 100644
--- a/lib/previews/index.ts
+++ b/lib/previews/index.ts
@@ -177,6 +177,8 @@ const registry: Record<string, RegistryEntry> = Object.fromEntries(
const configs = new Map<string, Promise<ResolvedComponentConfig>>();
/** Dev: each loaded config as its module exports it, to tell which ones a rebuild changed. */
const rawConfigs = new Map<string, unknown>();
+/** Dev: the configs a rebuild replaced, resolved. */
+const swapped = new Map<string, ResolvedComponentConfig>();
export function loadComponentConfig(
slug: string,
@@ -252,6 +254,17 @@ export function previewsRevision(): number {
return revision;
}
+/**
+ * Dev: the config of `slug` as the last rebuild that changed it left it, or
+ * undefined while the page has the one it loaded with. It is resolved, so a
+ * component can render with it at once (use-component-config.ts).
+ */
+export function swappedConfig(
+ slug: string,
+): ResolvedComponentConfig | undefined {
+ return swapped.get(slug);
+}
+
/** Dev: a rebuilt bundle is out. Replace what the page loaded from the old one. */
function swap(version: string) {
swapping = swapping
@@ -268,8 +281,10 @@ function swap(version: string) {
// Unchanged chunks keep their URL, so an unchanged config is the
// same object and its consumers are left alone.
if (fresh === undefined || fresh === rawConfigs.get(slug)) continue;
+ const resolved = await bundle.loadComponentConfig(slug);
rawConfigs.set(slug, fresh);
- configs.set(slug, bundle.loadComponentConfig(slug));
+ configs.set(slug, Promise.resolve(resolved));
+ swapped.set(slug, resolved);
} catch (error) {
console.error(`[previews] config of "${slug}" failed to load`, error);
}
diff --git a/lib/previews/use-component-config.ts b/lib/previews/use-component-config.ts
index 5c470c56..927238da 100644
--- a/lib/previews/use-component-config.ts
+++ b/lib/previews/use-component-config.ts
@@ -1,23 +1,26 @@
"use client";
-import { startTransition, use, useEffect, useState } from "react";
+import { use, useSyncExternalStore } from "react";
import type { ResolvedComponentConfig } from "@/lib/customizer-config";
-import { loadComponentConfig, onPreviewsUpdate } from "./index";
+import { loadComponentConfig, onPreviewsUpdate, swappedConfig } from "./index";
+
+const none = () => undefined;
/**
* The resolved config of a preview. Suspends until the config's chunk has
* loaded, then reads the cached promise synchronously; no other config is
* fetched.
*
- * In dev it renders again when a saved config is rebuilt. The update is a
- * transition, so the preview stays on screen while the new config loads
- * instead of falling back to its Suspense placeholder.
+ * In dev it renders again when a saved config is rebuilt. The rebuilt config
+ * comes resolved, so that is an ordinary render and the preview stays on
+ * screen. It is not a transition: one that suspends further down (the Player
+ * loading a composition again) starts over on every retry and never commits.
*/
export function useComponentConfig(slug: string): ResolvedComponentConfig {
- const [, refresh] = useState(0);
- useEffect(
- () => onPreviewsUpdate(() => startTransition(() => refresh((n) => n + 1))),
- [],
+ const swapped = useSyncExternalStore(
+ onPreviewsUpdate,
+ () => swappedConfig(slug),
+ none,
);
- return use(loadComponentConfig(slug));
+ return swapped ?? use(loadComponentConfig(slug));
}