From 55d4d8336edcebed79cb0cef9d343d29ae32115a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 23 Sep 2026 02:34:54 -0400 Subject: [PATCH] fix(cli): serve an edited text asset even when its size and mtime match The play page tagged every asset by mtime and size, so a same-size rewrite within one filesystem tick kept the old tag and the browser kept stale bytes. Text assets now get a content-hash tag; media keeps the mtime and size tag Studio already uses. --- packages/cli/src/commands/play.test.ts | 21 ++++++++++++++++++++- packages/cli/src/commands/play.ts | 8 ++++++++ packages/cli/src/utils/compositionServer.ts | 14 +++++++------- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/commands/play.test.ts b/packages/cli/src/commands/play.test.ts index 5fe38c9f45..55e2d9f395 100644 --- a/packages/cli/src/commands/play.test.ts +++ b/packages/cli/src/commands/play.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { Hono } from "hono"; @@ -175,6 +175,25 @@ describe("registerCompositionRoute", () => { expect(edited.status).toBe(200); }); + it("serves an edited stylesheet even when its size and mtime did not change", async () => { + const project = tmpProject(); + const css = join(project.dir, "style.css"); + const pinned = new Date("2026-01-01T00:00:00Z"); + writeFileSync(css, "a{color:red}"); + utimesSync(css, pinned, pinned); + const app = await buildApp(project, false); + const etag = (await app.request("/composition/style.css")).headers.get("ETag") ?? ""; + + writeFileSync(css, "a{color:tan}"); + utimesSync(css, pinned, pinned); + const edited = await app.request("/composition/style.css", { + headers: { "If-None-Match": etag }, + }); + + expect(edited.status).toBe(200); + expect(await edited.text()).toBe("a{color:tan}"); + }); + it("answers a Range request on a plain asset with 206 + the requested byte slice", async () => { const project = tmpProject(); writeFileSync(join(project.dir, "clip.mp4"), Buffer.from("0123456789", "utf-8")); diff --git a/packages/cli/src/commands/play.ts b/packages/cli/src/commands/play.ts index a956252468..7ecd9ee64c 100644 --- a/packages/cli/src/commands/play.ts +++ b/packages/cli/src/commands/play.ts @@ -236,6 +236,14 @@ export async function registerCompositionRoute( } const contentType = assetContentType(filePath); + // Edited text is caught by a content hash; an mtime tag can repeat for a same-size rewrite. + if (/^text\/|javascript|json|svg/.test(contentType)) { + return revalidatedResponse( + readFileSync(filePath, "utf-8"), + contentType, + ctx.req.header("If-None-Match"), + ); + } const proxyParam = ctx.req.query("hf-proxy"); if (proxyParam !== undefined && isProxyVariantRequest(proxyParam)) { // Opt-out (or a non-video asset) 404s the param without attempting a diff --git a/packages/cli/src/utils/compositionServer.ts b/packages/cli/src/utils/compositionServer.ts index 50ff93557b..09e7fb7f6c 100644 --- a/packages/cli/src/utils/compositionServer.ts +++ b/packages/cli/src/utils/compositionServer.ts @@ -82,13 +82,6 @@ export function assetContentType(filePath: string): string { return getMimeType(filePath); } -/** - * Hono-native Range/206 response for a file on disk, mirroring the inline - * Range logic in `packages/studio-server/src/routes/preview.ts`'s static - * asset route. `staticProjectServer.ts`'s raw-`node:http` counterpart is - * `serveFileWithRange`; this version converts Node's bounded file stream to a - * Fetch API stream for Hono without allocating the whole media/proxy file. - */ /** `body` with a content validator, or a 304 when the browser already holds these bytes. */ export function revalidatedResponse( body: string, @@ -101,6 +94,13 @@ export function revalidatedResponse( return new Response(body, { status: 200, headers }); } +/** + * Hono-native Range/206 response for a file on disk, mirroring the inline + * Range logic in `packages/studio-server/src/routes/preview.ts`'s static + * asset route. `staticProjectServer.ts`'s raw-`node:http` counterpart is + * `serveFileWithRange`; this version converts Node's bounded file stream to a + * Fetch API stream for Hono without allocating the whole media/proxy file. + */ export function buildRangeResponse( filePath: string, contentType: string,