Skip to content

🎥 Make videos programmatically with React

`@remotion/studio-server`: Fix infinite loop in visual mode (#7297)

Jonny Burgerbe17bcfb396a
diff --git a/packages/bundler/src/watch-ignore-next-change-plugin.ts b/packages/bundler/src/watch-ignore-next-change-plugin.ts
index 12d85c8859..d51ca63121 100644
--- a/packages/bundler/src/watch-ignore-next-change-plugin.ts
+++ b/packages/bundler/src/watch-ignore-next-change-plugin.ts
@@ -69,6 +69,7 @@ export class WatchIgnoreNextChangePlugin {
private dirsToIgnore = new Set<string>();
private snapshotFileTimestamps = new Map<string, TimeInfoEntry>();
private snapshotDirTimestamps = new Map<string, TimeInfoEntry>();
+ private suppressedFilesHistory = new Set<string>();
private currentWatcher: Watcher | null = null;
private trace: TraceLogFn;
@@ -118,6 +119,15 @@ export class WatchIgnoreNextChangePlugin {
this.snapshotDirTimestamps.delete(dir);
}
+ // Returns and clears the list of files whose changes were previously
+ // suppressed. Caller can use this to e.g. touch the files later and
+ // trigger a real rebuild.
+ consumeSuppressedFilesHistory(): string[] {
+ const files = [...this.suppressedFilesHistory];
+ this.suppressedFilesHistory.clear();
+ return files;
+ }
+
apply(compiler: Compiler): void {
compiler.hooks.afterEnvironment.tap('WatchIgnoreNextChangePlugin', () => {
const wfs = compiler.watchFileSystem as unknown as NodeWatchFileSystem;
@@ -192,6 +202,7 @@ export class WatchIgnoreNextChangePlugin {
if (wasInChanged) {
suppressedFiles.push(file);
+ self.suppressedFilesHistory.add(file);
self.filesToIgnore.delete(file);
self.snapshotFileTimestamps.delete(file);
}
diff --git a/packages/studio-server/src/preview-server/watch-ignore-next-change.ts b/packages/studio-server/src/preview-server/watch-ignore-next-change.ts
index bc6e6293e4..aaf78bb4ef 100644
--- a/packages/studio-server/src/preview-server/watch-ignore-next-change.ts
+++ b/packages/studio-server/src/preview-server/watch-ignore-next-change.ts
@@ -1,3 +1,4 @@
+import {promises as fs} from 'node:fs';
import type {WatchIgnoreNextChangePlugin} from '@remotion/bundler';
let currentPlugin: WatchIgnoreNextChangePlugin | null = null;
@@ -11,3 +12,30 @@ export const setWatchIgnoreNextChangePlugin = (
export const suppressBundlerUpdateForFile = (absolutePath: string): void => {
currentPlugin?.ignoreNextChange(absolutePath);
};
+
+// Why do we need this?
+// Consider we have a <Sequence>.
+// 1. In visual mode, we update it to layout='none'. This is reflected in the browser and in the code,
+// but Webpack is not aware because we suppressed the file change
+// 2. Reload the page. Sequence registers still as layout={undefined}, then listens to sequence props
+// asynchronously!
+// 3. Server says that layout="none" is the case, props gets updated, and this reloads the <Sequence>.
+// The same happens again - infinite loop!
+// --> When reloading the Studio, it is a good idea to reset Webpack to a non-hacked state.
+
+export const reloadPreviouslySuppressedFiles = async (): Promise<void> => {
+ if (!currentPlugin) {
+ return;
+ }
+
+ const files = currentPlugin.consumeSuppressedFilesHistory();
+
+ const now = new Date();
+ await Promise.all(
+ files.map(async (file) => {
+ try {
+ await fs.utimes(file, now, now);
+ } catch {}
+ }),
+ );
+};
diff --git a/packages/studio-server/src/routes.ts b/packages/studio-server/src/routes.ts
index 859f337faf..a83ec049c5 100644
--- a/packages/studio-server/src/routes.ts
+++ b/packages/studio-server/src/routes.ts
@@ -37,8 +37,8 @@ import type {LiveEventsServer} from './preview-server/live-events';
import {parseRequestBody} from './preview-server/parse-body';
import {fetchFolder, getFiles} from './preview-server/public-folder';
import {serveStatic} from './preview-server/serve-static';
+import {reloadPreviouslySuppressedFiles} from './preview-server/watch-ignore-next-change';
import type {RemotionConfigResponse} from './remotion-config-response';
-
const editorGuess = guessEditor();
const loggedStaticFileHints = new Set<string>();
@@ -105,6 +105,11 @@ const handleFallback = async ({
logLevel: LogLevel;
enableCrossSiteIsolation: boolean;
}) => {
+ const acceptsHtml = (request.headers.accept ?? '').includes('text/html');
+ if (request.method === 'GET' && acceptsHtml) {
+ await reloadPreviouslySuppressedFiles();
+ }
+
const requestUrl = new URL(request.url as string, 'http://localhost');
const {pathname} = requestUrl;
const staticFileHint = getStaticFileFallbackHint({