Production-ready animations, transitions, backgrounds, and scenes for Remotion
TypeScript ai-agentsclaude-codedemo-videomcpmcp-servermodel-context-protocolmotion-designproduct-demoreactremotionshadcnvideo
perf(dev): keep previews and element payloads out of the server build
kapishdima4073bca440b7
diff --git a/app/globals.css b/app/globals.cssindex 284749be..98a750ad 100644--- a/app/globals.css+++ b/app/globals.css@@ -5,6 +5,9 @@@import "fumadocs-ui/css/preset.css";@import "./typeset.css";@source "../node_modules/fumadocs-ui/dist/**/*.js";+/* Built registry JSON (27 MB) only repeats class names from registry/, which+ is scanned already: skipping it leaves the CSS byte-identical. */+@source not "../registry-artifacts";@custom-variant dark (&:is(.dark *));diff --git a/app/r/elements/[file]/route.ts b/app/r/elements/[file]/route.tsnew file mode 100644index 00000000..3da778e1--- /dev/null+++ b/app/r/elements/[file]/route.ts@@ -0,0 +1,44 @@+import { readFile } from "node:fs/promises";+import { join } from "node:path";+import { NextResponse } from "next/server";++// Remotion Studio element payloads (`registry-artifacts/elements/*.json`),+// fetched by the icon gallery and the caption picker when someone installs or+// drags an item. Read off disk on request so the 600+ payloads never enter a+// bundle: a dynamic `import()` over the folder made the bundler compile every+// one of them for both the browser and SSR on the first docs request.+export const runtime = "nodejs";++const ELEMENTS_DIR = join(process.cwd(), "registry-artifacts", "elements");++const ELEMENT_FILE = /^[a-z0-9][a-z0-9-]*\.json$/;++export async function GET(+ _request: Request,+ { params }: { params: Promise<{ file: string }> },+) {+ const { file } = await params;+ if (!ELEMENT_FILE.test(file)) return notFound();++ let payload: string;+ try {+ payload = await readFile(join(ELEMENTS_DIR, file), "utf8");+ } catch {+ return notFound();+ }++ return new NextResponse(payload, {+ headers: {+ "content-type": "application/json; charset=utf-8",+ // Payloads change only with a deploy.+ "cache-control": "public, max-age=3600",+ },+ });+}++function notFound() {+ return NextResponse.json(+ { error: "Unknown element.", code: "not_found" },+ { status: 404 },+ );+}diff --git a/bun.lock b/bun.lockindex 4d73eb89..633e4166 100644--- a/bun.lock+++ b/bun.lock@@ -51,6 +51,7 @@"rehype-pretty-code": "^0.14.4","remotion": "4.0.542","server-only": "^0.0.1",+ "shiki": "^4.0.2","sonner": "^2.0.7","tailwind-merge": "^3.5.0","takumi-js": "^2.3.0",diff --git a/components/docs/code-block.tsx b/components/docs/code-block.tsxindex 4222d276..a2cb6746 100644--- a/components/docs/code-block.tsx+++ b/components/docs/code-block.tsx@@ -1,11 +1,37 @@"use client";-import { DynamicCodeBlock } from "fumadocs-ui/components/dynamic-codeblock";+import { createShikiFactory } from "fumadocs-core/highlight/shiki";+import { DynamicCodeBlock } from "fumadocs-ui/components/dynamic-codeblock.core";import type { ComponentProps } from "react";import type { ShikiTransformer } from "shiki";import { CODE_PRE, CodeFrame } from "@/components/docs/blocks/code-frame";import { cn } from "@/lib/utils";+// Only what the snippets use. Fumadocs' default factory loads `shiki` whole,+// which bundles every grammar and theme (300+ chunks, ~30 MB in dev).+const LANGS = {+ tsx: () => import("shiki/langs/tsx.mjs"),+};++const shiki = createShikiFactory({+ async init() {+ const { createBundledHighlighter } = await import("shiki/core");+ const { createJavaScriptRegexEngine } = await import(+ "shiki/engine/javascript"+ );+ const createHighlighter = createBundledHighlighter({+ langs: LANGS,+ themes: {+ vesper: () => import("shiki/themes/vesper.mjs"),+ "github-light-default": () =>+ import("shiki/themes/github-light-default.mjs"),+ },+ engine: () => createJavaScriptRegexEngine(),+ });+ return createHighlighter({ langs: [], themes: [] });+ },+});+const transformers: ShikiTransformer[] = [{pre(node) {@@ -44,6 +70,7 @@ export function CodeBlock({bodyClassName={bodyClassName}><DynamicCodeBlock+ highlighter={() => shiki.getOrInit()}lang={lang}code={code}options={{diff --git a/components/docs/shader-preset-card.tsx b/components/docs/shader-preset-card.tsxindex 0675c4a0..41256131 100644--- a/components/docs/shader-preset-card.tsx+++ b/components/docs/shader-preset-card.tsx@@ -2,7 +2,7 @@import { Player, type PlayerRef } from "@remotion/player";import Link from "next/link";-import { useRef } from "react";+import { useEffect, useRef, useState } from "react";import { useAutoplay } from "@/app/(home)/components/use-autoplay";import type { ShaderPresetItem } from "@/lib/shader-presets";import registry from "@/registry/__index__";@@ -14,6 +14,11 @@ export function ShaderPresetCard({ item }: { item: ShaderPresetItem }) {const preview = previewManifest[item.name];const playerRef = useRef<PlayerRef>(null);const { containerRef } = useAutoplay(playerRef);+ // The Player mounts after hydration: on the server its lazyComponent would+ // load the shader there, and the server build strips preview loads+ // (lib/build/strip-server-preview-loads.cjs).+ const [mounted, setMounted] = useState(false);+ useEffect(() => setMounted(true), []);const command = `npx shadcn@latest add @remocn/${item.name}`;return (@@ -22,7 +27,9 @@ export function ShaderPresetCard({ item }: { item: ShaderPresetItem }) {ref={containerRef}className="surface-card aspect-video w-full overflow-hidden rounded-xl">- {entry && preview ? (+ {!entry || !preview ? (+ <div className="size-full bg-muted/40" />+ ) : mounted ? (<Playerref={playerRef}lazyComponent={entry.load}@@ -36,9 +43,7 @@ export function ShaderPresetCard({ item }: { item: ShaderPresetItem }) {loopacknowledgeRemotionLicense/>- ) : (- <div className="size-full bg-muted/40" />- )}+ ) : null}</div><div className="flex items-center justify-between gap-2"><Linkdiff --git a/lib/build/strip-server-preview-loads.cjs b/lib/build/strip-server-preview-loads.cjsnew file mode 100644index 00000000..811f5df2--- /dev/null+++ b/lib/build/strip-server-preview-loads.cjs@@ -0,0 +1,40 @@+// Turbopack loader for `registry/__index__.tsx`, applied to server builds only+// (see `turbopack.rules` in next.config.ts).+//+// Every registry entry has `load: () => import(<component>)`. Previews only+// ever call it in the browser: the Player mounts the composition client-side,+// and SSR renders the stage around it. But the bundler can't know that, so it+// compiled all ~900 components a second time for the server graph on the+// first request to any page that imports the index. Here each server-side+// `load` becomes a stub, which keeps every component out of the server graph.+// `loadConfig` is left alone: SSR does read configs.+//+// The stub rejects, so a server call fails loudly (the prerender errors) rather+// than rendering an empty frame. A Remotion <Player lazyComponent={load}>+// rendered during SSR calls `load` there: mount such a Player after hydration,+// as the docs stage, component cards and shader preset cards do.+//+// lib/build/strip-server-preview-loads.test.ts checks that this still matches+// every entry of the real index.++const LOAD_ENTRY =+ /^([ \t]+)load: \(\) =>\s+import\([\s\S]*?(?=^\1loadConfig:)/gm;++function stripServerPreviewLoads(source) {+ const out = source.replace(+ LOAD_ENTRY,+ (_match, indent) => `${indent}load: serverPreviewLoad,\n`,+ );+ return `${out}+function serverPreviewLoad() {+ return Promise.reject(+ new Error(+ "registry/__index__: load() is browser-only, the server build strips it (lib/build/strip-server-preview-loads.cjs)",+ ),+ );+}+`;+}++module.exports = stripServerPreviewLoads;+module.exports.LOAD_ENTRY = LOAD_ENTRY;diff --git a/lib/build/strip-server-preview-loads.test.ts b/lib/build/strip-server-preview-loads.test.tsnew file mode 100644index 00000000..14e4542b--- /dev/null+++ b/lib/build/strip-server-preview-loads.test.ts@@ -0,0 +1,37 @@+import { describe, expect, it } from "bun:test";+import { readFileSync } from "node:fs";+import { join } from "node:path";+import registry from "@/registry/__index__";+import stripServerPreviewLoads from "./strip-server-preview-loads.cjs";++const source = readFileSync(+ join(import.meta.dir, "../../registry/__index__.tsx"),+ "utf8",+);+const stripped = stripServerPreviewLoads(source);+const entryCount = Object.keys(registry).length;++const count = (text: string, pattern: RegExp) =>+ text.match(pattern)?.length ?? 0;++describe("strip-server-preview-loads", () => {+ it("replaces the load of every registry entry", () => {+ // A new entry written in a shape the loader doesn't match would put its+ // component back into the server graph without failing anywhere else.+ expect(count(stripped, /^\s+load: serverPreviewLoad,$/gm)).toBe(entryCount);+ });++ it("leaves no component import behind a load", () => {+ expect(stripped).not.toMatch(/load: \(\) =>\s+import\(/);+ });++ it("keeps every loadConfig", () => {+ expect(count(stripped, /loadConfig: \(\) =>\s+import\(/g)).toBe(entryCount);+ });++ it("keeps the RegistryEntry type intact", () => {+ expect(stripped).toContain(+ "load: () => Promise<{ default: React.ComponentType<any> }>;",+ );+ });+});diff --git a/lib/og-card.tsx b/lib/og-card.tsxindex 27c094e2..0a89cc34 100644--- a/lib/og-card.tsx+++ b/lib/og-card.tsx@@ -9,22 +9,28 @@ import { ImageResponse } from "takumi-js/response";* with its description at the bottom.*/-const asset = (p: string) => join(process.cwd(), p);-+// Literal `join(process.cwd(), "…")` calls, not a helper: the bundler traces+// a literal path to that one file, but a computed one to the whole project.const shaderDataUri = `data:image/png;base64,${readFileSync(- asset("public/landing/shader-magenta.png"),+ join(process.cwd(), "public/landing/shader-magenta.png"),).toString("base64")}`;const markDataUri = `data:image/svg+xml;base64,${Buffer.from(- readFileSync(asset("public/logo-mark.svg"), "utf8").replace(+ readFileSync(join(process.cwd(), "public/logo-mark.svg"), "utf8").replace(/currentColor/g,"#ffffff",),).toString("base64")}`;-const geist400 = readFileSync(asset("app/og/fonts/geist-400.woff2"));-const geist500 = readFileSync(asset("app/og/fonts/geist-500.woff2"));-const manrope600 = readFileSync(asset("app/og/fonts/manrope-600.woff2"));+const geist400 = readFileSync(+ join(process.cwd(), "app/og/fonts/geist-400.woff2"),+);+const geist500 = readFileSync(+ join(process.cwd(), "app/og/fonts/geist-500.woff2"),+);+const manrope600 = readFileSync(+ join(process.cwd(), "app/og/fonts/manrope-600.woff2"),+);export const OG_SIZE = { width: 1200, height: 630 } as const;diff --git a/lib/studio-element.ts b/lib/studio-element.tsindex 1e4f456d..876c0421 100644--- a/lib/studio-element.ts+++ b/lib/studio-element.ts@@ -1,5 +1,7 @@import type { StudioElementPayload } from "@remotion/studio-protocol";+// `null` = the item has no payload (404). Network failures are not cached so+// the next attempt retries.const cache = new Map<string, StudioElementPayload | null>();export function getCachedStudioElement(@@ -14,12 +16,18 @@ export async function loadStudioElement(const cached = cache.get(name);if (cached !== undefined) return cached;try {- const mod = await import(`@/registry-artifacts/elements/${name}.json`);- const payload = mod.default as StudioElementPayload;+ // Fetched, not `import()`ed: a dynamic import over the elements folder+ // compiles all 600+ payloads into the bundle (see app/r/elements).+ const res = await fetch(`/r/elements/${encodeURIComponent(name)}.json`);+ if (res.status === 404) {+ cache.set(name, null);+ return null;+ }+ if (!res.ok) return null;+ const payload = (await res.json()) as StudioElementPayload;cache.set(name, payload);return payload;} catch {- cache.set(name, null);return null;}}diff --git a/next.config.ts b/next.config.tsindex e310a4ca..813f5de3 100644--- a/next.config.ts+++ b/next.config.ts@@ -46,6 +46,18 @@ const nextConfig: NextConfig = {},turbopack: {root: __dirname,+ rules: {+ // Previews load their component in the browser only, so the server+ // build drops those imports from the preview index instead of compiling+ // ~900 components a second time. A `path` condition rather than a+ // path-glob key: Turbopack didn't apply the rule under a glob key.+ "*": {+ condition: {+ all: [{ not: "browser" }, { path: /^registry\/__index__\.tsx$/ }],+ },+ loaders: ["./lib/build/strip-server-preview-loads.cjs"],+ },+ },},async rewrites() {// Agents get Markdown at the same URLs; see lib/agent-routing.ts.diff --git a/package.json b/package.jsonindex 5ea2905e..347f4df7 100644--- a/package.json+++ b/package.json@@ -90,6 +90,7 @@"rehype-pretty-code": "^0.14.4","remotion": "4.0.542","server-only": "^0.0.1",+ "shiki": "^4.0.2","sonner": "^2.0.7","tailwind-merge": "^3.5.0","takumi-js": "^2.3.0",diff --git a/scripts/low-priority.mts b/scripts/low-priority.mtsindex dfb74d3e..7fac8cae 100644--- a/scripts/low-priority.mts+++ b/scripts/low-priority.mts@@ -1,10 +1,20 @@-// Runs a command below normal CPU priority, so the editor and browser stay-// responsive while Turbopack compiles. On macOS the utility QoS class keeps-// performance cores free for foreground apps at nearly the same compile-// speed (`-b`, background, confines it to efficiency cores and is ~6x slower).+// Runs a command below normal CPU priority (`nice -n 10`), so the editor and+// browser win any contention while Turbopack compiles, and an idle machine+// still gives it full speed. Not macOS QoS classes: `taskpolicy -c utility`+// throttles the process even on an idle machine and doubled the cold compile+// of the docs route (~16 s -> ~33 s on an M3), with no measurable gain for a+// foreground process; `-b` confines it to efficiency cores and is ~6x slower.+//+// It also caps Turbopack's parallelism at half the cores. Turbopack sizes its+// Node.js loader pool (MDX, PostCSS) from the core count: on an 8-core M3 that+// is ~12 processes of 120-200 MB each at the peak. Half the cores compiles the+// docs route just as fast with ~0.6 GB less peak memory. The variable is read+// by turbo-tasks inside Next's native binding and isn't documented; set it+// yourself to override.//// bun scripts/low-priority.mts next devimport { spawn } from "node:child_process";+import { availableParallelism } from "node:os";const command = process.argv.slice(2);if (command.length === 0) {@@ -13,13 +23,14 @@ if (command.length === 0) {}const [bin, ...args] =- process.platform === "darwin"- ? ["taskpolicy", "-c", "utility", ...command]- : process.platform === "linux"- ? ["nice", "-n", "10", ...command]- : command;+ process.platform === "win32" ? command : ["nice", "-n", "10", ...command];++const env = { ...process.env };+env.TURBO_TASKS_AVAILABLE_PARALLELISM ??= String(+ Math.max(2, Math.floor(availableParallelism() / 2)),+);-const child = spawn(bin, args, { stdio: "inherit" });+const child = spawn(bin, args, { stdio: "inherit", env });// Ctrl+C already reaches the child through the terminal's process group;// forward the rest so `kill` on this wrapper stops the server too.