Skip to content

Commit 6f4d46a

Browse files
fix(studio-server): a refused preview frame says so at once instead of timing out (#6074)
* fix(studio-server): a refused preview frame says so at once instead of timing out A frame the server refuses (project gone, bad variables) loaded a JSON body: no runtime and no error, so Studio waited three 15s budgets and reported 'waiting for runtime'. The refusal now reaches the frame as a page that reports it, like a failing preview script. * chore(studio-server): remove redundant preview refusal comments
1 parent 66f55ff commit 6f4d46a

3 files changed

Lines changed: 78 additions & 1 deletion

File tree

‎packages/studio-server/src/createStudioApi.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Hono } from "hono";
1+
import { Hono, type Context, type Next } from "hono";
22
import type { StudioApiAdapter } from "./types.js";
33
import { registerProjectRoutes } from "./routes/projects.js";
44
import { registerFileRoutes } from "./routes/files.js";
@@ -19,6 +19,19 @@ import { registerGlobalAssetRoutes } from "./routes/globalAssets.js";
1919
import { registerHistoryRoutes } from "./routes/history.js";
2020
import { replaceWithProjectDirMissing } from "./helpers/projectDirMissing.js";
2121
import { folderGone, isProjectRootMissing } from "./helpers/safePath.js";
22+
import { previewErrorPage } from "./helpers/previewErrorPage.js";
23+
24+
async function answerRefusedFrameWithPage(c: Context, next: Next) {
25+
await next();
26+
if (c.req.header("sec-fetch-dest") !== "iframe" || c.res.status < 400) return;
27+
const headers = new Headers(c.res.headers);
28+
headers.set("content-type", "text/html; charset=utf-8");
29+
headers.delete("content-length");
30+
c.res = new Response(previewErrorPage(c.res.status), {
31+
status: c.res.status,
32+
headers,
33+
});
34+
}
2235

2336
/**
2437
* Create a Hono sub-app with all studio API routes.
@@ -28,6 +41,8 @@ import { folderGone, isProjectRootMissing } from "./helpers/safePath.js";
2841
*/
2942
export function createStudioApi(adapter: StudioApiAdapter): Hono {
3043
const api = new Hono();
44+
api.use("/projects/:id/preview/*", answerRefusedFrameWithPage);
45+
api.use("/projects/:id/preview", answerRefusedFrameWithPage);
3146
api.use(async function answerProjectDirMissingAfterErrorHandlers(c, next) {
3247
const hostHeaders = new Headers(c.res.headers);
3348
await next();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { STUDIO_PREVIEW_ERRORS } from "@hyperframes/core/studio-preview-mark";
2+
3+
export function previewErrorPage(status: number): string {
4+
const message = `The preview page could not be loaded (HTTP ${status}).`;
5+
return `<!doctype html><meta charset="utf-8"><script>window.${STUDIO_PREVIEW_ERRORS}=[${JSON.stringify(message)}]</script><p>${message}</p>`;
6+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// @vitest-environment node
2+
import { describe, expect, it } from "vitest";
3+
import { STUDIO_PREVIEW_ERRORS } from "@hyperframes/core/studio-preview-mark";
4+
import { createStudioApi } from "../createStudioApi";
5+
import type { StudioApiAdapter } from "../types";
6+
7+
const adapter = (found: boolean): StudioApiAdapter => ({
8+
listProjects: () => [],
9+
resolveProject: async (id: string) => (found ? { id, dir: "/nowhere/" + id } : null),
10+
bundle: async () => null,
11+
lint: async () => ({ findings: [] }),
12+
runtimeUrl: "/api/runtime.js",
13+
rendersDir: () => "/tmp/renders",
14+
startRender: () => ({
15+
id: "job",
16+
status: "rendering",
17+
progress: 0,
18+
outputPath: "/tmp/out.mp4",
19+
}),
20+
});
21+
22+
const frame = { headers: { "sec-fetch-dest": "iframe" } };
23+
24+
describe("a preview page the server cannot serve", () => {
25+
it("tells the frame it loaded, so the player reports it instead of waiting for a runtime", async () => {
26+
const res = await createStudioApi(adapter(false)).request(
27+
"http://localhost/projects/gone/preview",
28+
frame,
29+
);
30+
31+
expect(res.status).toBe(404);
32+
expect(res.headers.get("content-type")).toContain("text/html");
33+
const page = await res.text();
34+
expect(page).toContain(`window.${STUDIO_PREVIEW_ERRORS}=[`);
35+
expect(page).toContain("404");
36+
});
37+
38+
it("does the same for a sub-composition page", async () => {
39+
const res = await createStudioApi(adapter(false)).request(
40+
"http://localhost/projects/gone/preview/comp/scene.html",
41+
frame,
42+
);
43+
44+
expect(res.headers.get("content-type")).toContain("text/html");
45+
expect(await res.text()).toContain(`window.${STUDIO_PREVIEW_ERRORS}=[`);
46+
});
47+
48+
it("keeps the JSON answer for a request that is not a frame navigation", async () => {
49+
const res = await createStudioApi(adapter(false)).request(
50+
"http://localhost/projects/gone/preview",
51+
);
52+
53+
expect(res.status).toBe(404);
54+
expect(await res.json()).toEqual({ error: "not found" });
55+
});
56+
});

0 commit comments

Comments
 (0)