Skip to content

🎥 Make videos programmatically with React

Elements: Add Markdown content negotiation for SSR

Igor Samokhovetsf4d3fc059f9e
diff --git a/bun.lock b/bun.lock
index 654754b76c..2cf3ab203d 100644
--- a/bun.lock
+++ b/bun.lock
@@ -2154,6 +2154,7 @@
"@remotion/player": "workspace:*",
"@remotion/studio-protocol": "workspace:*",
"isbot": "5.1.40",
+ "negotiator": "1.1.0",
"react": "19.2.7",
"react-dom": "19.2.7",
"react-router": "8.3.0",
@@ -2166,6 +2167,7 @@
"@remotion/eslint-config-flat": "workspace:*",
"@remotion/renderer": "workspace:*",
"@tailwindcss/vite": "4.3.3",
+ "@types/negotiator": "0.6.5",
"@types/node": "22.20.0",
"@types/react": "19.2.7",
"@types/react-dom": "19.2.3",
@@ -5599,6 +5601,8 @@
"@types/mute-stream": ["@types/[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-CPM9nzrCPPJHQNA9keH9CVkVI+WR5kMa+7XEs5jcGQ0VoAGnLv242w8lIVgwAEfmE4oufJRaTc9PNLQl0ioAow=="],
+ "@types/negotiator": ["@types/[email protected]", "", {}, "sha512-MPOlB48mfWhoUlynY0ga7CFsXIPcH6vGPkjzXMn2p+4PH1QUyn2KPtw0hrLLmO6SaX4zse3X6h2x/083vveAlA=="],
+
"@types/nlcst": ["@types/[email protected]", "", { "dependencies": { "@types/unist": "2.0.6" } }, "sha512-vSYNSDe6Ix3q+6Z7ri9lyWqgGhJTmzRjZRqyq15N0Z/1/UnVsno9G/N40NBijoYx2seFDIl0+B2mgAb9mezUCA=="],
"@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "5.26.5" } }, "sha512-scnD59RpYD91xngrQQLGkE+6UrHUPzeKZWhhjBSa3HSkwjbQc38+q3RoIVEwxQGRw3M+j5hpNAM+lgV3cVormg=="],
@@ -7611,7 +7615,7 @@
"natural-compare": ["[email protected]", "", {}, "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw=="],
- "negotiator": ["[email protected]", "", {}, "sha512-myRT3DiWPHqho5PrJaIRyaMv2kgYf0mUVgBNOYMuCH5Ki1yEiQaf/ZJuQ62nvpc44wL5WDbTX7yGJi1Neevw8w=="],
+ "negotiator": ["[email protected]", "", { "dependencies": { "content-type": "^2.1.0" } }, "sha512-NMPBRMJgiQHjbd8phG3Vebdx4kZ1H121rbl5IkMqeOsahptB9BKo/d7oJ3zTXqTgagn2bWlNSXkh0QUGM31RYg=="],
"neo-async": ["[email protected]", "", {}, "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw=="],
@@ -11563,6 +11567,8 @@
"compression/debug": ["[email protected]", "", { "dependencies": { "ms": "2.0.0" } }, "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA=="],
+ "compression/negotiator": ["[email protected]", "", {}, "sha512-myRT3DiWPHqho5PrJaIRyaMv2kgYf0mUVgBNOYMuCH5Ki1yEiQaf/ZJuQ62nvpc44wL5WDbTX7yGJi1Neevw8w=="],
+
"concaveman/robust-predicates": ["[email protected]", "", {}, "sha512-l4NwboJM74Ilm4VKfbAtFeGq7aEjWL+5kVFcmgFA2MrdnQWx9iE/tUGvxY5HyMI7o/WpSIUFLbC5fbeaHgSCYg=="],
"concaveman/tinyqueue": ["[email protected]", "", {}, "sha512-ppJZNDuKGgxzkHihX8v9v9G5f+18gzaTfrukGrq6ueg0lmH4nqVnA2IPG0AEH3jKEk2GRJCUhDoqpoiw3PHLBA=="],
@@ -12015,6 +12021,8 @@
"make-fetch-happen/minipass": ["[email protected]", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw=="],
+ "make-fetch-happen/negotiator": ["[email protected]", "", {}, "sha512-myRT3DiWPHqho5PrJaIRyaMv2kgYf0mUVgBNOYMuCH5Ki1yEiQaf/ZJuQ62nvpc44wL5WDbTX7yGJi1Neevw8w=="],
+
"make-fetch-happen/ssri": ["[email protected]", "", { "dependencies": { "minipass": "^3.1.1" } }, "sha512-o57Wcn66jMQvfHG1FlYbWeZWW/dHZhJXjpIcTfXldXEk5nz5lStPo3mK0OJQfGR3RbZUlbISexbljkJzuEj/8Q=="],
"maplibre-gl/@maplibre/maplibre-gl-style-spec": ["@maplibre/[email protected]", "", { "dependencies": { "@mapbox/jsonlint-lines-primitives": "^2.0.3", "@mapbox/unitbezier": "^1.0.0", "json-stringify-pretty-compact": "^4.0.0", "minimist": "^1.2.8", "quickselect": "^3.0.0", "tinyqueue": "^3.0.0" }, "bin": { "gl-style-format": "dist/gl-style-format.mjs", "gl-style-migrate": "dist/gl-style-migrate.mjs", "gl-style-validate": "dist/gl-style-validate.mjs" } }, "sha512-6J0vZqMZvRAJKtdWJdDGHEh1YJ2ZHG08/GOur8gCArYhO8ZkM//OXqtI2AAEz4jc2G+Wq4MfcePg8qNK5TZ4Kg=="],
@@ -12187,6 +12195,8 @@
"multimatch/minimatch": ["[email protected]", "", { "dependencies": { "brace-expansion": "^1.1.7" } }, "sha512-VgjWUsnnT6n+NUk6eZq77zeFdpW2LWDzP6zFGrCbHXiYNul5Dzqk2HHQ5uFH2DNW5Xbp8+jVzaeNt94ssEEl4w=="],
+ "negotiator/content-type": ["[email protected]", "", {}, "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag=="],
+
"next/sharp": ["[email protected]", "", { "dependencies": { "@img/colour": "^1.1.0", "detect-libc": "^2.1.2", "semver": "^7.8.5" }, "optionalDependencies": { "@img/sharp-darwin-arm64": "0.35.4", "@img/sharp-darwin-x64": "0.35.4", "@img/sharp-freebsd-wasm32": "0.35.4", "@img/sharp-libvips-darwin-arm64": "1.3.3", "@img/sharp-libvips-darwin-x64": "1.3.3", "@img/sharp-libvips-linux-arm": "1.3.3", "@img/sharp-libvips-linux-arm64": "1.3.3", "@img/sharp-libvips-linux-ppc64": "1.3.3", "@img/sharp-libvips-linux-riscv64": "1.3.3", "@img/sharp-libvips-linux-s390x": "1.3.3", "@img/sharp-libvips-linux-x64": "1.3.3", "@img/sharp-libvips-linuxmusl-arm64": "1.3.3", "@img/sharp-libvips-linuxmusl-x64": "1.3.3", "@img/sharp-linux-arm": "0.35.4", "@img/sharp-linux-arm64": "0.35.4", "@img/sharp-linux-ppc64": "0.35.4", "@img/sharp-linux-riscv64": "0.35.4", "@img/sharp-linux-s390x": "0.35.4", "@img/sharp-linux-x64": "0.35.4", "@img/sharp-linuxmusl-arm64": "0.35.4", "@img/sharp-linuxmusl-x64": "0.35.4", "@img/sharp-webcontainers-wasm32": "0.35.4", "@img/sharp-win32-arm64": "0.35.4", "@img/sharp-win32-ia32": "0.35.4", "@img/sharp-win32-x64": "0.35.4" } }, "sha512-n++8XWcj+jCOr2IOl7h8LbKnGBDY4aPbmprMONBNFdn0ImXqpGVv5zliDs0V9HbmbCQLpbuo2ej9rAoOQTvMDA=="],
"no-case/tslib": ["[email protected]", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="],
diff --git a/packages/template-element-library/README.md b/packages/template-element-library/README.md
index 0f68f2dbaa..081440da00 100644
--- a/packages/template-element-library/README.md
+++ b/packages/template-element-library/README.md
@@ -1,6 +1,6 @@
# Remotion Element Library
-A React Router + Tailwind CSS starter for publishing [Remotion Elements](https://www.remotion.dev/elements). Includes a prerendered static website, an authoring Studio, and four editable examples. See the [third-party Element libraries](https://www.remotion.dev/elements/libraries) for examples of published catalogs.
+Use this React Router and Tailwind CSS starter to publish [Remotion Elements](https://www.remotion.dev/elements). The starter includes a prerendered static website, an authoring Studio, and four editable examples. For published catalogs, see [third-party Element libraries](https://www.remotion.dev/elements/libraries).
## Develop
@@ -11,7 +11,7 @@ npm install
npm run dev
```
-In another terminal, start the authoring Studio:
+Start the authoring Studio in another terminal:
```sh
npm run remotion:studio
@@ -21,18 +21,27 @@ For exports and upgrades, see [`npx remotion render`](https://www.remotion.dev/d
## Customize
-- `app/`: website routes, layout, and styles. Replace “Studio Supply” with your library name.
-- `elements/`: self-contained components and the shared catalog registry.
-- `remotion/`: authoring compositions and the preview stage.
-- `lib/element-payload.ts`: raw source loading and Studio Protocol payloads.
+- `app/` contains website routes, layout, and styles. Replace "Studio Supply" with your library name.
+- `elements/` contains the Element components and the catalog registry. Each Element component is self-contained.
+- `remotion/` contains authoring compositions and the preview stage.
+- `lib/element-payload.ts` loads raw source files and creates Studio Protocol payloads.
-To add an Element, create `elements/<slug>/<slug>.tsx`, add its metadata to `elements/registry.ts`, map its component in `remotion/ElementPreview.tsx`, and register its authoring composition in `remotion/Root.tsx`. The detail page, preview renderer, and Studio use the same source files. Gallery previews are rendered assets, not live Players.
+Do these steps to add an Element:
+
+1. Create `elements/<slug>/<slug>.tsx`.
+2. Add the Element metadata to `elements/registry.ts`.
+3. Map the Element component in `remotion/ElementPreview.tsx`.
+4. Register the authoring composition in `remotion/Root.tsx`.
+
+The detail page, preview renderer, and Studio use the same source files. The gallery uses rendered previews. It does not use live Players.
See the [Element guidelines](https://www.remotion.dev/elements/guidelines), [interactivity documentation](https://www.remotion.dev/docs/studio/interactivity), and [library integration guide](https://www.remotion.dev/docs/studio-protocol/component-library-integration) for layout, assets, dependencies, and installation requirements.
## Render and upload previews
-The starter previews are hosted on `remotion.media`. The gallery and caption picker use posters and hover/focus videos from `elements/previews.json`. Rendering uses a separate entry point, `remotion/previews.tsx`, with dimensions and default props from the registry. Source edits appear in the listing after you rerender.
+`remotion.media` hosts the example previews. The gallery and caption picker use `elements/previews.json` for posters and videos. The videos play on hover or focus.
+
+The `remotion/previews.tsx` entry point renders previews. The renderer uses dimensions and default props from the registry. After you change an Element, render its previews again.
```sh
npm run render-previews
@@ -40,9 +49,13 @@ npm run render-previews
npm run render-previews -- --element=animated-title
```
-This renders PNG posters and muted MP4 videos into ignored `public/previews/` directories and updates the manifest to local URLs. Transparent previews use WebM instead of MP4. Review the results with `npm run dev`. Playback failures and reduced-motion preferences retain the poster. Pass `--browser-executable=/path/to/chrome` to use an existing browser.
+The script writes PNG posters and muted MP4 videos to `public/previews/`. Git ignores this directory. The script updates the manifest with local URLs. The script uses WebM instead of MP4 for transparent previews.
+
+Use `npm run dev` to check the previews. If playback fails or the user prefers reduced motion, the poster stays visible. To use an existing browser, add `--browser-executable=/path/to/chrome` to the command.
-To publish previews, copy `.env.example` to `.env` and configure your own S3-compatible bucket, endpoint, public HTTPS URL, and credentials. For R2, enable a [public domain](https://developers.cloudflare.com/r2/buckets/public-buckets/) for the bucket. Credentials are used only by the upload script; never prefix them with `VITE_`.
+To upload previews, copy `.env.example` to `.env`. Set the bucket, endpoint, public HTTPS URL, and credentials for your S3-compatible storage. If you use R2, enable a [public domain](https://developers.cloudflare.com/r2/buckets/public-buckets/) for the bucket.
+
+Only the upload script uses these credentials. Do not add `VITE_` to the start of credential names.
```sh
npm run upload-previews
@@ -50,13 +63,21 @@ npm run upload-previews
npm run upload-previews -- --element=animated-title
```
-Uploads use unique URLs and verify public content types and SHA-256 checksums before updating `elements/previews.json`. Existing hosted assets are never overwritten. Commit the updated manifest, then rebuild the website. Local renders remain ignored and can be removed after upload; shipping local previews instead is also supported.
+The upload script uses unique URLs. The script checks the public files for correct content types and SHA-256 checksums. Then it updates `elements/previews.json`. It does not overwrite existing hosted assets.
+
+Commit the updated manifest. Build the website again.
+
+Git ignores local preview files. You can delete these files after the upload. You can also deploy local previews with the website without an upload.
## Caption styles
-`/captions` is a separate gallery and Studio caption-style picker. Library registration includes this URL as `captionStylesUrl`. In Studio's captions context, the page hides navigation, uses a transparent background, and reflects selection and Reset through [`subscribeToCaptionStyleSelection()`](https://www.remotion.dev/docs/studio-protocol/subscribe-to-caption-style-selection).
+The `/captions` page is a gallery and a Studio caption-style picker. Register its URL as the `captionStylesUrl` for your library.
+
+In the Studio captions context, the page hides navigation and uses a transparent background. The page uses [`subscribeToCaptionStyleSelection()`](https://www.remotion.dev/docs/studio-protocol/subscribe-to-caption-style-selection) to respond to selection and Reset.
-Accent captions remixes [Word Highlight Captions](https://www.remotion.dev/elements/captions/word-highlight-captions) with a system font and warm accent. Caption Elements accept `Caption[]`, expose `Interactive.captionsSchema`, and use component-owned installation with `isCaptionStyle: true`. Set their registry category to `Captions` to include them in the picker.
+Accent captions uses [Word Highlight Captions](https://www.remotion.dev/elements/captions/word-highlight-captions) as its base. It has a system font and warm accent.
+
+Caption Elements accept `Caption[]` and expose `Interactive.captionsSchema`. They use component-owned installation with `isCaptionStyle: true`. Set the registry category to `Captions` to show an Element in the picker.
## Build and deploy
@@ -66,26 +87,39 @@ npm run build
npm run preview
```
-Publish `build/client` to a static host. All Element pages are prerendered from the registry. See the [React Router prerendering guide](https://reactrouter.com/how-to/pre-rendering) for static hosting and fallback routing. Use HTTPS and allow embedding in Remotion Studio, as described in the library integration guide.
+The default build prerenders all Element pages from the registry. Publish `build/client` to a static host. See the [React Router prerendering guide](https://reactrouter.com/how-to/pre-rendering) for static hosting and fallback routing.
+
+Serve the website with HTTPS. Allow embedding in Remotion Studio. See the library integration guide for embedding requirements.
## Markdown and agent discovery
-Pages with Markdown declare its URL with a `<link rel="alternate" type="text/markdown">` tag. Agents can follow `/index.md` from the homepage to find every Element without guessing URLs or running JavaScript.
+Each page with Markdown has a `<link rel="alternate" type="text/markdown">` tag. The tag gives the Markdown URL.
+
+Agents can follow `/index.md` from the homepage to find every Element. They do not need to guess URLs or run JavaScript.
+
+`app/routes.ts` registers the Markdown resource routes. HTML-only pages do not need a Markdown route.
+
+HTML and Markdown use the same Element registry, installation payloads, and source files. When you add an Element, it appears in both formats.
+
+The static build writes `.md` files to `build/client`. These files do not need a production server or host integration.
+
+Configure your host to serve `.md` files directly. If possible, set `Content-Type: text/markdown; charset=utf-8`. The routes set headers during development. The host sets headers in production.
-Markdown is served by resource routes registered in `app/routes.ts`. HTML-only pages need no Markdown route.
+### Content negotiation with SSR
-Markdown uses the website's Element registry, installation payloads, and source files. New Elements get both formats automatically. Builds write `.md` files to `build/client`; you don't need a production server or host integration.
+To use SSR:
-Serve `.md` files directly, preferably with `Content-Type: text/markdown; charset=utf-8`. The routes set development headers; your host sets production headers.
+1. Set `const ssr = true` in `react-router.config.ts`.
+2. Deploy with an SSR adapter for your host.
-### Optional content negotiation
+This setting disables prerendering. React Router handles page requests instead of serving static HTML.
-Static files cannot inspect `Accept` headers. To serve Markdown at HTML URLs when a client prefers `text/markdown`, configure your host or server to rewrite each page URL to its Markdown file.
+The server middleware in `app/routes/layout.tsx` checks the `Accept` header. It returns Markdown at page URLs when the client prefers `text/markdown`. It uses quality values to select the format. It returns HTML when the browser prefers HTML.
-Keep HTML for browser requests. Respect quality values: an HTML preference or `text/markdown;q=0` must not select Markdown. Rewrite only existing pages on GET/HEAD, normalize trailing slashes, and leave assets and `.md` URLs unchanged. Set `Vary: Accept` on both responses and configure your CDN to cache them separately, or disable caching for these URLs.
+The middleware supports negotiation for GET and HEAD requests. It does not change data requests. Both formats include the `Vary: Accept` response header. Configure your CDN to use this header for its cache.
-Direct `.md` links work without negotiation.
+Direct `.md` links also work on static hosts. For content negotiation, configure routing rules on the static host. Application code does not run when a static host serves these files.
## License
-Remotion’s [license terms](https://www.remotion.dev/license) apply. Choose a license for the Elements you publish.
+The [license terms](https://www.remotion.dev/license) for Remotion apply. Choose a license for the Elements you publish.
diff --git a/packages/template-element-library/app/routes/layout.tsx b/packages/template-element-library/app/routes/layout.tsx
index 6571ee3f42..6278971672 100644
--- a/packages/template-element-library/app/routes/layout.tsx
+++ b/packages/template-element-library/app/routes/layout.tsx
@@ -1,7 +1,55 @@
import { isInsideStudio } from "@remotion/studio-protocol";
+import Negotiator from "negotiator";
import { useEffect, useState } from "react";
import { Link, Outlet, useLocation } from "react-router";
+import { catalog } from "../../lib/element-payload";
+import { getCatalogMarkdown, getElementMarkdown } from "../../lib/markdown";
import { StudioActions } from "../components/studio-actions";
+import type { Route } from "./+types/layout";
+
+export const middleware: Route.MiddlewareFunction[] = [
+ async ({ request, pattern, params }, next) => {
+ if (
+ (request.method !== "GET" && request.method !== "HEAD") ||
+ new URL(request.url).pathname.endsWith(".data")
+ ) {
+ return next();
+ }
+
+ const markdownType = "text/markdown; charset=utf-8";
+ const preferredType = new Negotiator({
+ headers: { accept: request.headers.get("accept") ?? "" },
+ }).mediaType(["text/html", markdownType]);
+
+ let markdown: string | null = null;
+ if (preferredType === markdownType) {
+ switch (pattern) {
+ case "/":
+ markdown = getCatalogMarkdown(null);
+ break;
+ case "captions":
+ markdown = getCatalogMarkdown("Captions");
+ break;
+ case "elements/:slug": {
+ const entry = catalog.find(
+ ({ definition }) => definition.slug === params.slug,
+ );
+ markdown = entry ? getElementMarkdown(entry) : null;
+ break;
+ }
+ }
+ }
+
+ const response =
+ markdown === null
+ ? await next()
+ : new Response(markdown, {
+ headers: { "Content-Type": markdownType },
+ });
+ response.headers.append("Vary", "Accept");
+ return response;
+ },
+];
export default function LibraryLayout() {
const location = useLocation();
diff --git a/packages/template-element-library/package.json b/packages/template-element-library/package.json
index 14ef6da840..9fb4bb088d 100644
--- a/packages/template-element-library/package.json
+++ b/packages/template-element-library/package.json
@@ -2,9 +2,9 @@
"name": "template-element-library",
"private": true,
"type": "module",
- "engines": {
- "node": "^22.22.0 || >=24"
- },
+ "sideEffects": [
+ "*.css"
+ ],
"scripts": {
"dev": "react-router dev",
"build": "react-router build",
@@ -15,9 +15,6 @@
"render-previews": "node scripts/render-previews.ts",
"upload-previews": "node --env-file-if-exists=.env scripts/upload-previews.ts"
},
- "sideEffects": [
- "*.css"
- ],
"dependencies": {
"@heroicons/react": "2.2.0",
"@react-router/node": "8.3.0",
@@ -25,27 +22,32 @@
"@remotion/cli": "workspace:*",
"@remotion/player": "workspace:*",
"@remotion/studio-protocol": "workspace:*",
+ "isbot": "5.1.40",
+ "negotiator": "1.1.0",
"react": "19.2.7",
"react-dom": "19.2.7",
"react-router": "8.3.0",
- "remotion": "workspace:*",
- "isbot": "5.1.40"
+ "remotion": "workspace:*"
},
"devDependencies": {
"@aws-sdk/client-s3": "3.986.0",
"@react-router/dev": "8.3.0",
"@remotion/bundler": "workspace:*",
- "@remotion/renderer": "workspace:*",
"@remotion/eslint-config-flat": "workspace:*",
+ "@remotion/renderer": "workspace:*",
+ "@tailwindcss/vite": "4.3.3",
+ "@types/negotiator": "0.6.5",
"@types/node": "22.20.0",
"@types/react": "19.2.7",
"@types/react-dom": "19.2.3",
"eslint": "10.12.0",
"prettier": "3.8.1",
"sirv-cli": "3.0.1",
- "typescript": "5.9.3",
- "vite": "8.3.4",
"tailwindcss": "4.3.3",
- "@tailwindcss/vite": "4.3.3"
+ "typescript": "5.9.3",
+ "vite": "8.3.4"
+ },
+ "engines": {
+ "node": "^22.22.0 || >=24"
}
}
diff --git a/packages/template-element-library/react-router.config.ts b/packages/template-element-library/react-router.config.ts
index f86327c714..3054297521 100644
--- a/packages/template-element-library/react-router.config.ts
+++ b/packages/template-element-library/react-router.config.ts
@@ -1,13 +1,17 @@
import type { Config } from "@react-router/dev/config";
import { elements } from "./elements/registry";
+const ssr = false;
+
export default {
- ssr: false,
- prerender: ({ getStaticPaths }) => [
- ...getStaticPaths(),
- ...elements.flatMap(({ slug }) => [
- `/elements/${slug}`,
- `/elements/${slug}.md`,
- ]),
- ],
+ ssr,
+ prerender: ssr
+ ? false
+ : ({ getStaticPaths }) => [
+ ...getStaticPaths(),
+ ...elements.flatMap(({ slug }) => [
+ `/elements/${slug}`,
+ `/elements/${slug}.md`,
+ ]),
+ ],
} satisfies Config;