Skip to content

🎥 Make videos programmatically with React

only run calculateMetadata() once

Jonny Burgerec74f1a0a589
diff --git a/packages/bundler/src/homepage/homepage.tsx b/packages/bundler/src/homepage/homepage.tsx
index 969be8dfa3..622614a867 100644
--- a/packages/bundler/src/homepage/homepage.tsx
+++ b/packages/bundler/src/homepage/homepage.tsx
@@ -1,5 +1,4 @@
import React, {useCallback, useEffect, useState} from 'react';
-import type {AnyCompMetadata} from 'remotion';
import {getBundleMode} from '../bundle-mode';
import {setBundleModeAndUpdate} from '../renderEntry';
@@ -32,7 +31,7 @@ type CompositionState =
}
| {
type: 'loaded';
- comps: AnyCompMetadata[];
+ comps: string[];
}
| {
type: 'error';
@@ -53,13 +52,12 @@ const AvailableCompositions: React.FC = () => {
const check = () => {
if (window.remotion_renderReady === true) {
setComps({type: 'loading'});
-
- window
- .getStaticCompositions()
- .then((newComps) => {
- setComps({type: 'loaded', comps: newComps});
- })
- .catch((err) => ({type: 'error', error: err}));
+ try {
+ const newComps = window.remotion_getCompositionNames();
+ setComps({type: 'loaded', comps: newComps});
+ } catch (err) {
+ setComps({type: 'error', error: err as Error});
+ }
} else {
timeout = setTimeout(check, 250);
}
@@ -106,7 +104,7 @@ const AvailableCompositions: React.FC = () => {
{state === null
? []
: state.comps.map((c) => {
- return <li key={c.id}>{c.id}</li>;
+ return <li key={c}>{c}</li>;
})}
</ul>
</div>
diff --git a/packages/bundler/src/renderEntry.tsx b/packages/bundler/src/renderEntry.tsx
index 02ee82e8ce..a1a55e5456 100644
--- a/packages/bundler/src/renderEntry.tsx
+++ b/packages/bundler/src/renderEntry.tsx
@@ -226,7 +226,7 @@ export const setBundleModeAndUpdate = (state: BundleState) => {
};
if (typeof window !== 'undefined') {
- window.getStaticCompositions = (): Promise<AnyCompMetadata[]> => {
+ const getUnevaluatedComps = () => {
if (!Internals.getRoot()) {
throw new Error(
'registerRoot() was never called. 1. Make sure you specified the correct entrypoint for your bundle. 2. If your registerRoot() call is deferred, use the delayRender/continueRender pattern to tell Remotion to wait.'
@@ -261,6 +261,12 @@ if (typeof window !== 'undefined') {
);
}
+ return compositions;
+ };
+
+ window.getStaticCompositions = (): Promise<AnyCompMetadata[]> => {
+ const compositions = getUnevaluatedComps();
+
return Promise.all(
compositions.map((c): Promise<AnyCompMetadata> => {
return Promise.resolve(
@@ -274,12 +280,12 @@ if (typeof window !== 'undefined') {
);
};
- window.calculateComposition = (compId: string) => {
- if (!Internals.compositionsRef.current) {
- throw new Error('Unexpectedly did not have a CompositionManager');
- }
+ window.remotion_getCompositionNames = () => {
+ return getUnevaluatedComps().map((c) => c.id);
+ };
- const compositions = Internals.compositionsRef.current.getCompositions();
+ window.calculateComposition = (compId: string) => {
+ const compositions = getUnevaluatedComps();
const selectedComp = compositions.find((c) => c.id === compId);
if (!selectedComp) {
throw new Error(`Could not find composition with ID ${compId}`);
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 4d8d734374..045f75f4fd 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -12,6 +12,7 @@ declare global {
interface Window {
remotion_renderReady: boolean;
remotion_cancelledError: string | undefined;
+ remotion_getCompositionNames: () => string[];
getStaticCompositions: () => Promise<AnyCompMetadata[]>;
calculateComposition: (compId: string) => Promise<VideoConfig>;
setBundleMode: (bundleMode: BundleState) => void;