🎥 Make videos programmatically with React
feat: watchStaticFile()
mrkirthi-24cff1584069f4
diff --git a/packages/cli/src/event-source.ts b/packages/cli/src/event-source.tsindex 7da410f9d7..7893754766 100644--- a/packages/cli/src/event-source.ts+++ b/packages/cli/src/event-source.ts@@ -51,6 +51,10 @@ export const openEventSource = () => {}if (newEvent.type === 'new-public-folder') {+ const updatedFilesEvent = new CustomEvent('watch_remotion_staticFiles', {+ detail: { files: newEvent.files },+ });+ window.dispatchEvent(updatedFilesEvent);window.remotion_staticFiles = newEvent.files;window.remotion_publicFolderExists = newEvent.folderExists;}diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex ed0a50343d..d19b1b9a00 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -133,6 +133,7 @@ export * from './use-video-config.js';export * from './version.js';export * from './video-config.js';export * from './video/index.js';+export {watchStaticFile} from './watch-static-file.js';export const Experimental = {/**diff --git a/packages/core/src/watch-static-file.ts b/packages/core/src/watch-static-file.tsnew file mode 100644index 0000000000..21eca869cb--- /dev/null+++ b/packages/core/src/watch-static-file.ts@@ -0,0 +1,58 @@+import {getRemotionEnvironment} from './get-remotion-environment';+import type {StaticFile} from './get-static-files';++type WatcherCallback = (newData: StaticFile | null) => void;++/**+ * @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.+ * @see [Documentation](https://www.remotion.dev/docs/watchstaticfile)+ */+export const watchStaticFile = (+ fileName: string,+ callback: WatcherCallback,+): (() => 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 () => {};+ }++ let prevFileData = window.remotion_staticFiles.find(+ (file: StaticFile) => file.name === fileName,+ );++ // Check if the specified static file has updated or deleted+ const checkFile = (event: Event) => {+ const staticFiles: StaticFile[] = (event as CustomEvent).detail.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 () => {};+ }+ } else {+ callback(null); // File is deleted+ }+ };++ window.addEventListener('watch_remotion_staticFiles', checkFile);++ const cancel = () => {+ return window.removeEventListener('watch_remotion_staticFiles', checkFile);+ };++ return cancel;+};diff --git a/packages/docs/docs/watch-static-file.md b/packages/docs/docs/watch-static-file.mdnew file mode 100644index 0000000000..72358e568a--- /dev/null+++ b/packages/docs/docs/watch-static-file.md@@ -0,0 +1,67 @@+---+image: /generated/articles-docs-watch-static-file.png+id: watchstaticfile+title: watchStaticFile()+crumb: "API"+---++_Available from v4.0.60._++Watches for changes in a specific static file 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.+:::++## 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++```tsx twoslash title="example.tsx"+import { StaticFile, watchStaticFile } from "remotion";++// Watch for changes in a specific static file+const cancelWatcher = watchStaticFile(+ "your-static-file.jpg",+ (newData: StaticFile | null) => {+ if (newData) {+ console.log(`File ${newData.name} has been added or modified.`);+ } else {+ console.log("File has been deleted.");+ }+ },+);++// To stop watching for changes, call the cancel function+// cancelWatcher();+```++## See also++- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/watch-static-file.ts)diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex eb0543c2dc..1c1ca2ab2b 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -107,6 +107,7 @@ module.exports = {"clipper","get-remotion-environment","version",+ "watchstaticfile",],},{diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex 3b711a025d..81004c40d5 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -2752,6 +2752,13 @@ export const articles = [compId: "articles-docs-visualize-audio",crumb: "@remotion/media-utils",},+ {+ id: "watchstaticfile",+ title: "watchStaticFile()",+ relativePath: "docs/watch-static-file.md",+ compId: "articles-docs-watch-static-file",+ crumb: "API",+ },{id: "wrong-composition-mount",title: "Wrongly mounted <Composition>",diff --git a/packages/docs/static/generated/articles-docs-watch-static-file.png b/packages/docs/static/generated/articles-docs-watch-static-file.pngnew file mode 100644index 0000000000..1a18229554Binary files /dev/null and b/packages/docs/static/generated/articles-docs-watch-static-file.png differ