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.
This commit is contained in:
Miguel Ángel
2026-09-23 08:05:27 -04:00
parent 97edcc1774
commit 55d4d8336e
3 changed files with 35 additions and 8 deletions
+20 -1
View File
@@ -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"));
+8
View File
@@ -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
+7 -7
View File
@@ -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,