Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(studio-server): preview keeps its base URL when a script mentions a <base> tag (#4426)
Miguel Ángel6a5fcd4cf2f8
diff --git a/packages/studio-server/src/helpers/subComposition.test.ts b/packages/studio-server/src/helpers/subComposition.test.tsindex ac82d98082..2e08e0798e 100644--- a/packages/studio-server/src/helpers/subComposition.test.ts+++ b/packages/studio-server/src/helpers/subComposition.test.ts@@ -3,7 +3,7 @@ import { mkdtempSync, mkdirSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { describe, expect, it } from "vitest";-import { buildSubCompositionHtml } from "./subComposition";+import { buildSubCompositionHtml, hasBaseElement } from "./subComposition";function makeTempProject(files: Record<string, string>): string {const dir = mkdtempSync(join(tmpdir(), "hf-subcomp-preview-"));@@ -16,6 +16,20 @@ function makeTempProject(files: Record<string, string>): string {}describe("buildSubCompositionHtml", () => {+ it("adds the preview base even when the project head's script mentions a <base>", () => {+ const dir = makeTempProject({+ "index.html": `<!doctype html><html><head><script>if (0) document.write('<base href="../">');</script></head><body></body></html>`,+ "compositions/scene.html": `<div data-composition-id="scene" data-width="320" data-height="180"></div>`,+ });+ const html = buildSubCompositionHtml(+ dir,+ "compositions/scene.html",+ "/api/runtime.js",+ "/api/projects/demo/preview/",+ );+ expect(html).toContain('<base href="/api/projects/demo/preview/">');+ });+it("handles full HTML document compositions without nesting <html> in <body>", () => {const dir = makeTempProject({"index.html": `<!doctype html>@@ -416,3 +430,19 @@ describe("buildSubCompositionHtml", () => {expect(html).toContain('src="assets/logo.png"');});});++describe("hasBaseElement", () => {+ it("counts a real <base> element and nothing that only mentions one", () => {+ expect(hasBaseElement(`<head><base href="/cdn/"></head>`)).toBe(true);+ expect(hasBaseElement(`<head><BASE HREF="/cdn/"></head>`)).toBe(true);+ for (const html of [+ `<head><script>document.write('<base href="../">')</script></head>`,+ `<head><script >x = "<base>";</script ></head>`,+ `<head><!-- <base href="x"> --></head>`,+ `<head><template><base href="t"></template></head>`,+ `<head><basefont></head>`,+ ]) {+ expect(hasBaseElement(html)).toBe(false);+ }+ });+});diff --git a/packages/studio-server/src/helpers/subComposition.ts b/packages/studio-server/src/helpers/subComposition.tsindex d9e9031500..c2eb2ec5c7 100644--- a/packages/studio-server/src/helpers/subComposition.ts+++ b/packages/studio-server/src/helpers/subComposition.ts@@ -334,7 +334,7 @@ export function buildSubCompositionHtml(}// Inject <base> for relative asset resolution (before other tags)- if (baseHref && !headContent.includes("<base")) {+ if (baseHref && !hasBaseElement(headContent)) {headContent = `<base href="${baseHref}">\n${headContent}`;}@@ -375,3 +375,8 @@ ${rewrittenContent}</body></html>`;}++/** True for a real `<base>` element; the text "<base" inside a script or comment does not count. */+export function hasBaseElement(html: string): boolean {+ return parseHTML(html).document.querySelector("base") !== null;+}diff --git a/packages/studio-server/src/routes/preview.test.ts b/packages/studio-server/src/routes/preview.test.tsindex 6150dbd4fd..cfff79fbbf 100644--- a/packages/studio-server/src/routes/preview.test.ts+++ b/packages/studio-server/src/routes/preview.test.ts@@ -82,6 +82,25 @@ async function getPreviewSignature(projectDir: string): Promise<string> {}describe("registerPreviewRoutes", () => {+ it("adds its <base> even when a script mentions one, and keeps an authored <base>", async () => {+ const projectDir = createProjectDir();+ const app = new Hono();+ registerPreviewRoutes(app, createAdapter(projectDir));+ writeFileSync(+ join(projectDir, "index.html"),+ `<!doctype html><html><head><script>if (0) document.write('<base href="../">');</script></head><body></body></html>`,+ );+ const injected = await (await app.request("http://localhost/projects/demo/preview")).text();+ expect(injected).toContain('<base href="/api/projects/demo/preview/">');++ writeFileSync(+ join(projectDir, "index.html"),+ `<!doctype html><html><head><base href="/cdn/"></head><body></body></html>`,+ );+ const authored = await (await app.request("http://localhost/projects/demo/preview")).text();+ expect(authored).not.toContain('<base href="/api/projects/demo/preview/">');+ });+it("injects Studio GSAP motion manifest runtime into project preview", async () => {const projectDir = createProjectDir();writeFileSync(diff --git a/packages/studio-server/src/routes/preview.ts b/packages/studio-server/src/routes/preview.tsindex 8baa1666df..e1d2ee8bc9 100644--- a/packages/studio-server/src/routes/preview.ts+++ b/packages/studio-server/src/routes/preview.ts@@ -12,7 +12,7 @@ import { isWithinProjectRoot } from "@hyperframes/parsers/asset-resolution";import type { StudioApiAdapter } from "../types.js";import { resolveWithinProject } from "../helpers/safePath.js";import { getMimeType } from "../helpers/mime.js";-import { buildSubCompositionHtml } from "../helpers/subComposition.js";+import { buildSubCompositionHtml, hasBaseElement } from "../helpers/subComposition.js";import {resolveProjectAndSignature,resolveProjectSignature,@@ -383,7 +383,7 @@ export function registerPreviewRoutes(api: Hono, adapter: PreviewApiAdapter): vo// Inject <base> for relative asset resolutionconst baseHref = `/api/projects/${project.id}/preview/`;- if (!bundled.includes("<base")) {+ if (!hasBaseElement(bundled)) {bundled = bundled.replace(/<head>/i, `<head><base href="${baseHref}">`);}