Skip to content

🎥 Make videos programmatically with React

docs and constants

Jonny Burgerbb543bcedb87
diff --git a/packages/cli/src/event-source.ts b/packages/cli/src/event-source.ts
index 43a31c8a17..ea27180187 100644
--- a/packages/cli/src/event-source.ts
+++ b/packages/cli/src/event-source.ts
@@ -1,3 +1,5 @@
+import type {WatchRemotionStaticFilesPayload} from 'remotion';
+import {Internals} from 'remotion';
import {sendErrorNotification} from './editor/components/Notifications/NotificationCenter';
import {renderJobsRef} from './editor/components/RenderQueue/context';
import {studioServerConnectionRef} from './editor/helpers/client-id';
@@ -51,10 +53,15 @@ export const openEventSource = () => {
}
if (newEvent.type === 'new-public-folder') {
- const updatedFilesEvent = new CustomEvent('watch_remotion_staticFiles', {
- detail: {files: newEvent.files},
- });
- window.dispatchEvent(updatedFilesEvent);
+ const payload: WatchRemotionStaticFilesPayload = {
+ files: newEvent.files,
+ };
+
+ window.dispatchEvent(
+ new CustomEvent(Internals.WATCH_REMOTION_STATIC_FILES, {
+ detail: payload,
+ }),
+ );
window.remotion_staticFiles = newEvent.files;
window.remotion_publicFolderExists = newEvent.folderExists;
}
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index fcb28c281f..78a35c4bbe 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -85,6 +85,8 @@ import {
useMediaMutedState,
useMediaVolumeState,
} from './volume-position-state.js';
+import type {WatchRemotionStaticFilesPayload} from './watch-static-file.js';
+import {WATCH_REMOTION_STATIC_FILES} from './watch-static-file.js';
import {
RemotionContextProvider,
useRemotionContexts,
@@ -154,6 +156,7 @@ export const Internals = {
NativeLayersProvider,
ClipComposition,
isIosSafari,
+ WATCH_REMOTION_STATIC_FILES,
} as const;
export type {
@@ -170,4 +173,5 @@ export type {
SetMediaVolumeContextValue,
RemotionEnvironment,
SerializedJSONWithCustomFields,
+ WatchRemotionStaticFilesPayload,
};
diff --git a/packages/core/src/watch-static-file.ts b/packages/core/src/watch-static-file.ts
index 21eca869cb..f8888fcaed 100644
--- a/packages/core/src/watch-static-file.ts
+++ b/packages/core/src/watch-static-file.ts
@@ -3,21 +3,27 @@ import type {StaticFile} from './get-static-files';
type WatcherCallback = (newData: StaticFile | null) => void;
+export const WATCH_REMOTION_STATIC_FILES = 'remotion_staticFilesChanged';
+
+export type WatchRemotionStaticFilesPayload = {
+ files: StaticFile[];
+};
+
/**
* @description Watch for changes in a specific static file.
* @param {string} fileName - The name of the static file to watch for changes.
* @param {WatcherCallback} callback - A callback function to be called when the file changes.
- * @returns {() => void} A function that can be used to cancel the event listener.
+ * @returns {{cancel: () => void}} A function that can be used to cancel the event listener.
* @see [Documentation](https://www.remotion.dev/docs/watchstaticfile)
*/
export const watchStaticFile = (
fileName: string,
callback: WatcherCallback,
-): (() => void) => {
+): {cancel: () => void} => {
// Check if function is called in Remotion Studio
if (!getRemotionEnvironment().isStudio) {
console.warn('The API is only available while using the Remotion Studio.');
- return () => {};
+ return {cancel: () => undefined};
}
let prevFileData = window.remotion_staticFiles.find(
@@ -26,33 +32,39 @@ export const watchStaticFile = (
// Check if the specified static file has updated or deleted
const checkFile = (event: Event) => {
- const staticFiles: StaticFile[] = (event as CustomEvent).detail.files;
+ const staticFiles: StaticFile[] = (
+ (event as CustomEvent).detail as WatchRemotionStaticFilesPayload
+ ).files;
// Check for user specified file
const newFileData = staticFiles.find(
(file: StaticFile) => file.name === fileName,
);
- if (newFileData) {
- if (
- prevFileData === undefined ||
- prevFileData.lastModified !== newFileData.lastModified
- ) {
- callback(newFileData); // File is added or modified
- prevFileData = newFileData;
- } else {
- return () => {};
+ if (!newFileData) {
+ // File is deleted
+ if (prevFileData !== undefined) {
+ callback(null);
}
- } else {
- callback(null); // File is deleted
+
+ prevFileData = undefined;
+ return;
+ }
+
+ if (
+ prevFileData === undefined ||
+ prevFileData.lastModified !== newFileData.lastModified
+ ) {
+ callback(newFileData); // File is added or modified
+ prevFileData = newFileData;
}
};
- window.addEventListener('watch_remotion_staticFiles', checkFile);
+ window.addEventListener(WATCH_REMOTION_STATIC_FILES, checkFile);
const cancel = () => {
- return window.removeEventListener('watch_remotion_staticFiles', checkFile);
+ return window.removeEventListener(WATCH_REMOTION_STATIC_FILES, checkFile);
};
- return cancel;
+ return {cancel};
};
diff --git a/packages/docs/docs/get-static-files.md b/packages/docs/docs/get-static-files.md
index ec771be192..52a3706eea 100644
--- a/packages/docs/docs/get-static-files.md
+++ b/packages/docs/docs/get-static-files.md
@@ -74,3 +74,4 @@ For performance, only the first 1000 items are fetched and displayed.
- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/get-static-files.ts)
- [`staticFile()`](/docs/staticfile)
+- [`watchStaticFile()`](/docs/watchstaticfile)
diff --git a/packages/docs/docs/staticfile.md b/packages/docs/docs/staticfile.md
index 38cf3f4b08..c624f8016b 100644
--- a/packages/docs/docs/staticfile.md
+++ b/packages/docs/docs/staticfile.md
@@ -94,3 +94,4 @@ The image will now be loaded properly, however, you must avoid to encode the fil
- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/static-file.ts)
- [Loading assets](/docs/assets)
- [`getStaticFiles()`](/docs/getstaticfiles)
+- [`watchStaticFile()`](/docs/watchstaticfile)
diff --git a/packages/docs/docs/watch-static-file.md b/packages/docs/docs/watch-static-file.md
index 72358e568a..b3b054849c 100644
--- a/packages/docs/docs/watch-static-file.md
+++ b/packages/docs/docs/watch-static-file.md
@@ -5,49 +5,21 @@ title: watchStaticFile()
crumb: "API"
---
-_Available from v4.0.60._
+# watchStaticFile()<AvailableFrom v="4.0.61"/>
-Watches for changes in a specific static file and invokes a callback function when the file changes, enabling dynamic updates in your Remotion projects.
+Watches for changes in a specific [static file](/docs/staticfile) and invokes a callback function when the file changes, enabling dynamic updates in your Remotion projects.
:::warning
-This feature is only available within the Remotion Studio environment.
+This feature is only available within the Remotion Studio environment. In the Player, events will never fire.
:::
-## Function Signature
-
-```ts twoslash
-import { watchStaticFile } from "remotion";
-import type {StaticFile} from './get-static-files';
-
-type WatcherCallback = (newData: StaticFile | null) => void;
-
-const cancel = watchStaticFile(
- filename: string,
- callback: WatcherCallback
-): () => void;
-
-//cancel()
-```
-
-## Arguments
-
-Takes two arguments and returns a function that can be used to `cancel` the event listener.
-
-### `filename`
-
-A name of the file in `/public` folder to watch for changes.
-
-### `callback`
-
-A callback function that will be called when the file is modified.
-
-### Example
+## Example
```tsx twoslash title="example.tsx"
import { StaticFile, watchStaticFile } from "remotion";
// Watch for changes in a specific static file
-const cancelWatcher = watchStaticFile(
+const { cancel } = watchStaticFile(
"your-static-file.jpg",
(newData: StaticFile | null) => {
if (newData) {
@@ -59,9 +31,23 @@ const cancelWatcher = watchStaticFile(
);
// To stop watching for changes, call the cancel function
-// cancelWatcher();
+cancel();
```
+## Arguments
+
+Takes two arguments and returns a function that can be used to `cancel` the event listener.
+
+### `filename`
+
+A name of the file in `/public` folder to watch for changes.
+
+### `callback`
+
+A callback function that will be called when the file is modified. As an argument, a [`StaticFile`](/docs/getstaticfiles#api) or `null` is passed.
+
## See also
- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/watch-static-file.ts)
+- [`staticFile()`](/docs/staticfile)
+- [`getStaticFiles()`](/docs/getstaticfiles)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 1c1ca2ab2b..7c5c7eca5d 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -90,6 +90,9 @@ module.exports = {
"staticfile",
"getstaticfiles",
"prefetch",
+ "get-remotion-environment",
+ "version",
+ "watchstaticfile",
"audio",
"sequence",
"loop",
@@ -105,9 +108,6 @@ module.exports = {
"easing",
"null",
"clipper",
- "get-remotion-environment",
- "version",
- "watchstaticfile",
],
},
{
@@ -447,10 +447,7 @@ module.exports = {
type: "doc",
id: "layout-utils/index",
},
- items: [
- "layout-utils/measure-text",
- "layout-utils/fill-text-box",
- ],
+ items: ["layout-utils/measure-text", "layout-utils/fill-text-box"],
},
],
mainSidebar: [