🎥 Make videos programmatically with React
signal that remotion version has disconnected
Jonny Burger9f1abbc2eec7
diff --git a/packages/bundler/src/bundle.ts b/packages/bundler/src/bundle.tsindex 2a155904c4..772c5cb86f 100644--- a/packages/bundler/src/bundle.ts+++ b/packages/bundler/src/bundle.ts@@ -134,6 +134,7 @@ export const bundle = async (editorName: null,inputProps: null,remotionRoot: resolvedRemotionRoot,+ previewServerCommand: null,});fs.writeFileSync(path.join(outDir, 'index.html'), html);diff --git a/packages/bundler/src/index-html.ts b/packages/bundler/src/index-html.tsindex fbcf157d60..21f6997454 100644--- a/packages/bundler/src/index-html.ts+++ b/packages/bundler/src/index-html.ts@@ -6,12 +6,14 @@ export const indexHtml = ({inputProps,staticHash,remotionRoot,+ previewServerCommand,}: {staticHash: string;baseDir: string;editorName: string | null;inputProps: object | null;remotionRoot: string;+ previewServerCommand: string | null;}) =>`<!DOCTYPE html>@@ -36,9 +38,12 @@ export const indexHtml = ({path.basename(remotionRoot))};</script><script>window.remotion_cwd = ${JSON.stringify(remotionRoot)};</script>+ <script>window.remotion_previewServerCommand = ${+ previewServerCommand ? JSON.stringify(previewServerCommand) : 'null'+ };</script>${inputProps- ? ` <script>window.remotion_inputProps = ${JSON.stringify(+ ? `<script>window.remotion_inputProps = ${JSON.stringify(JSON.stringify(inputProps))};</script>`diff --git a/packages/cli/src/editor/components/Editor.tsx b/packages/cli/src/editor/components/Editor.tsxindex 9f2f1739ee..744bfccd5b 100644--- a/packages/cli/src/editor/components/Editor.tsx+++ b/packages/cli/src/editor/components/Editor.tsx@@ -44,6 +44,7 @@ import {KeyboardShortcuts} from './KeyboardShortcutsModal';import NewComposition from './NewComposition/NewComposition';import {NoRegisterRoot} from './NoRegisterRoot';import {NotificationCenter} from './Notifications/NotificationCenter';+import {ServerDisconnected} from './Notifications/ServerDisconnected';import {UpdateModal} from './UpdateModal/UpdateModal';import {ZoomPersistor} from './ZoomPersistor';@@ -221,6 +222,7 @@ export const Editor: React.FC = () => {</TimelineZoomContext><NotificationCenter />+ <ServerDisconnected />{modalContextType &&modalContextType.type === 'new-comp' && (<NewCompositiondiff --git a/packages/cli/src/editor/components/Notifications/ServerDisconnected.tsx b/packages/cli/src/editor/components/Notifications/ServerDisconnected.tsxnew file mode 100644index 0000000000..2d366b59a7--- /dev/null+++ b/packages/cli/src/editor/components/Notifications/ServerDisconnected.tsx@@ -0,0 +1,83 @@+import React, {createRef, useImperativeHandle, useState} from 'react';++const container: React.CSSProperties = {+ position: 'absolute',+ justifyContent: 'center',+ alignItems: 'center',+ display: 'flex',+ width: '100%',+ flexDirection: 'column',+ paddingTop: 20,+ pointerEvents: 'none',+ backgroundColor: 'transparent',+ fontFamily: 'SF Pro, Arial, Helvetica, sans-serif',+};++const message: React.CSSProperties = {+ backgroundColor: '#e74c3c',+ color: 'white',+ paddingLeft: 16,+ paddingRight: 16,+ paddingTop: 8,+ paddingBottom: 8,+ borderRadius: 4,+ boxShadow: '0 2px 4px rgba(0, 0, 0, 0.4)',+ textAlign: 'center',+};++const inlineCode: React.CSSProperties = {+ fontSize: 16,+ fontFamily: 'monospace',+};++export const serverDisconnectedRef = createRef<{+ setServerDisconnected: () => void;+}>();++let pageIsGoingToReload = false;+window.addEventListener('beforeunload', () => {+ pageIsGoingToReload = true;+});++export const ServerDisconnected: React.FC = () => {+ const [serverDisconnected, setServerDisconnected] = useState(false);++ useImperativeHandle(+ serverDisconnectedRef,+ () => {+ return {+ setServerDisconnected: () => {+ setServerDisconnected(true);+ },+ };+ },+ []+ );++ if (!serverDisconnected) {+ return null;+ }++ if (pageIsGoingToReload) {+ return null;+ }++ return (+ <div style={container} className="css-reset">+ <div style={message}>+ The preview server has disconnected. <br />+ {window.remotion_previewServerCommand ? (+ <span>+ Run{' '}+ <code style={inlineCode}>+ {window.remotion_previewServerCommand}+ </code>{' '}+ to run it again.+ </span>+ ) : (+ <span>Fast refresh will not work.</span>+ )}+ </div>+ </div>+ );+};diff --git a/packages/cli/src/event-source.ts b/packages/cli/src/event-source.tsindex 05656b6560..40138f12f3 100644--- a/packages/cli/src/event-source.ts+++ b/packages/cli/src/event-source.ts@@ -1,3 +1,4 @@+import {serverDisconnectedRef} from './editor/components/Notifications/ServerDisconnected';import type {EventSourceEvent} from './event-source-events';let source: EventSource | null = null;@@ -11,4 +12,9 @@ export const openEventSource = () => {window.location.reload();}});++ source.addEventListener('error', () => {+ // Display an error message that the preview server has disconnected.+ serverDisconnectedRef.current?.setServerDisconnected();+ });};diff --git a/packages/cli/src/preview-server/get-package-manager.ts b/packages/cli/src/preview-server/get-package-manager.tsindex 626a912c1b..4ffe58453f 100644--- a/packages/cli/src/preview-server/get-package-manager.ts+++ b/packages/cli/src/preview-server/get-package-manager.ts@@ -6,19 +6,27 @@ type LockfilePath = {manager: PackageManager;path: string;installCommand: string;+ startCommand: string;};export const lockFilePaths: LockfilePath[] = [- {path: 'package-lock.json', manager: 'npm', installCommand: 'npm i'},+ {+ path: 'package-lock.json',+ manager: 'npm',+ installCommand: 'npm i',+ startCommand: 'npm start',+ },{path: 'yarn.lock',manager: 'yarn',installCommand: 'yarn add',+ startCommand: 'yarn start',},{path: 'pnpm-lock.yaml',manager: 'pnpm',installCommand: 'pnpm i',+ startCommand: 'pnpm start',},];diff --git a/packages/cli/src/preview-server/routes.ts b/packages/cli/src/preview-server/routes.tsindex 1f26c2d5f5..5e1afa881d 100644--- a/packages/cli/src/preview-server/routes.ts+++ b/packages/cli/src/preview-server/routes.ts@@ -10,6 +10,7 @@ import {launchEditor,} from './error-overlay/react-overlay/utils/open-in-editor';import type {SymbolicatedStackFrame} from './error-overlay/react-overlay/utils/stack-frame';+import {getPackageManager} from './get-package-manager';import type {LiveEventsServer} from './live-events';import {getProjectInfo} from './project-info';import {serveStatic} from './serve-static';@@ -51,6 +52,7 @@ const handleFallback = async ({response.setHeader('content-type', 'text/html');response.writeHead(200);+ const packageManager = getPackageManager(remotionRoot);response.end(BundlerInternals.indexHtml({staticHash: hash,@@ -58,6 +60,8 @@ const handleFallback = async ({editorName: displayName,inputProps: getCurrentInputProps(),remotionRoot,+ previewServerCommand:+ packageManager === 'unknown' ? null : packageManager.startCommand,}));};diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex c0ead4d99f..9f4b00deff 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -11,6 +11,7 @@ declare global {remotion_editorName: string | null;remotion_projectName: string;remotion_cwd: string;+ remotion_previewServerCommand: string;remotion_setFrame: (frame: number) => void;remotion_initialFrame: number;remotion_proxyPort: number;