Skip to content

🎥 Make videos programmatically with React

Wait for syntax colors before revealing Studio error details

Jonny Burgere06653c33528
diff --git a/packages/studio/src/components/LazySyntaxHighlightedSource.tsx b/packages/studio/src/components/LazySyntaxHighlightedSource.tsx
index e587d8d5cd..82375d02eb 100644
--- a/packages/studio/src/components/LazySyntaxHighlightedSource.tsx
+++ b/packages/studio/src/components/LazySyntaxHighlightedSource.tsx
@@ -1,19 +1,51 @@
import React, {lazy, Suspense} from 'react';
-const SyntaxHighlightedSource = lazy(() => import('./SyntaxHighlightedSource'));
-
-const fallbackStyle: React.CSSProperties = {
- visibility: 'hidden',
+const sourceStyle: React.CSSProperties = {
fontFamily: 'inherit',
fontSize: 'inherit',
lineHeight: 'inherit',
+ color: 'inherit',
};
-export const LazySyntaxHighlightedSource: React.FC<{
+type SourceComponent = React.FC<{
readonly source: string;
-}> = ({source}) => {
+}>;
+
+const PlainSource: SourceComponent = ({source}) => (
+ <span style={sourceStyle}>{source}</span>
+);
+
+let loadedSource: SourceComponent | null = null;
+let sourcePromise: Promise<{default: SourceComponent}> | null = null;
+
+export const loadSyntaxHighlightedSource = () => {
+ if (sourcePromise === null) {
+ sourcePromise = import('./SyntaxHighlightedSource')
+ .catch(() => ({default: PlainSource}))
+ .then((module) => {
+ loadedSource = module.default;
+ return module;
+ });
+ }
+
+ return sourcePromise;
+};
+
+const SyntaxHighlightedSource = lazy(loadSyntaxHighlightedSource);
+
+export const LazySyntaxHighlightedSource: SourceComponent = ({source}) => {
+ // React.lazy can suspend on its first render even after preloading finishes.
+ const LoadedSource = loadedSource;
+ if (LoadedSource !== null) {
+ return <LoadedSource source={source} />;
+ }
+
return (
- <Suspense fallback={<span style={fallbackStyle}>{source}</span>}>
+ <Suspense
+ fallback={
+ <span style={{...sourceStyle, visibility: 'hidden'}}>{source}</span>
+ }
+ >
<SyntaxHighlightedSource source={source} />
</Suspense>
);
diff --git a/packages/studio/src/error-overlay/remotion-overlay/ErrorLoader.tsx b/packages/studio/src/error-overlay/remotion-overlay/ErrorLoader.tsx
index b4e2ef6071..44b5a29c8b 100644
--- a/packages/studio/src/error-overlay/remotion-overlay/ErrorLoader.tsx
+++ b/packages/studio/src/error-overlay/remotion-overlay/ErrorLoader.tsx
@@ -1,5 +1,6 @@
import {getLocationFromBuildError} from '@remotion/studio-shared';
import React, {useEffect, useState} from 'react';
+import {loadSyntaxHighlightedSource} from '../../components/LazySyntaxHighlightedSource';
import {registerCurrentError} from '../current-error';
import {wasErrorLoggedByServer} from '../error-origin';
import type {ErrorRecord} from '../react-overlay/listen-to-runtime-errors';
@@ -94,15 +95,27 @@ export const ErrorLoader: React.FC<{
});
useEffect(() => {
+ let cancelled = false;
+ setState({type: 'loading'});
+ const highlighting = loadSyntaxHighlightedSource();
const symbolication = getErrorRecord(error);
const unregister = registerCurrentError({error, symbolication});
symbolication
- .then((record) => {
+ .then(async (record) => {
+ if (cancelled) {
+ return;
+ }
+
if (record) {
if (shouldLogError(error)) {
logSymbolicatedStudioError(record);
}
+ await highlighting;
+ if (cancelled) {
+ return;
+ }
+
setState({
type: 'symbolicated',
record,
@@ -118,6 +131,10 @@ export const ErrorLoader: React.FC<{
}
})
.catch((err) => {
+ if (cancelled) {
+ return;
+ }
+
if (shouldLogError(error)) {
logStudioError(error);
}
@@ -128,7 +145,10 @@ export const ErrorLoader: React.FC<{
});
});
- return unregister;
+ return () => {
+ cancelled = true;
+ unregister();
+ };
}, [error]);
if (state.type === 'loading') {