perf(studio): request the preview document at page load

Studio asked for the preview only once the player mounted, about 640 ms into a
first open. It now requests it the moment the page loads, and again when the
hash names another project, so the build or cache read overlaps Studio's boot.
This commit is contained in:
Miguel Ángel
2026-09-23 21:15:13 -04:00
parent 087398efc1
commit 38b2fa13c8
3 changed files with 41 additions and 0 deletions
+3
View File
@@ -5,8 +5,11 @@ import { StudioApp } from "./App";
import { StudioErrorBoundary } from "./components/StudioErrorBoundary";
import { readIconTokens } from "./styles/iconTokens";
import { trackStudioEvent } from "./utils/studioTelemetry";
import { prefetchPreviewForHash } from "./utils/previewPrefetch";
import "./styles/studio.css";
prefetchPreviewForHash(window.location.hash);
window.addEventListener("hashchange", () => prefetchPreviewForHash(window.location.hash));
trackStudioEvent("session_start");
function errorProps(value: unknown): {
@@ -0,0 +1,22 @@
// @vitest-environment happy-dom
import { afterEach, describe, expect, it, vi } from "vitest";
import { prefetchPreviewForHash } from "./previewPrefetch";
describe("prefetchPreviewForHash", () => {
afterEach(() => vi.unstubAllGlobals());
it("requests the opening project's preview once, not on every hash update", () => {
const fetchStub = vi.fn(() => Promise.resolve(new Response("")));
vi.stubGlobal("fetch", fetchStub);
prefetchPreviewForHash("#project/demo");
prefetchPreviewForHash("#project/demo?t=2&tab=design");
prefetchPreviewForHash("#project/other");
prefetchPreviewForHash("#settings");
expect(fetchStub.mock.calls.map(([url]) => new URL(String(url)).pathname)).toEqual([
"/api/projects/demo/preview",
"/api/projects/other/preview",
]);
});
});
@@ -0,0 +1,16 @@
import { applyPreviewVariablesToUrl } from "../hooks/previewVariablesStore";
import { buildProjectApiPath, parseProjectIdFromHash } from "./projectRouting";
let prefetched: string | null = null;
/** Asks the server for the preview document of the project in the URL while Studio boots, so
* the build runs in parallel with the app instead of after the player mounts. */
export function prefetchPreviewForHash(hash: string): void {
const projectId = parseProjectIdFromHash(hash);
if (!projectId || projectId === prefetched) return;
prefetched = projectId;
const url = new URL(buildProjectApiPath(projectId, "/preview"), window.location.origin);
applyPreviewVariablesToUrl(url);
// ponytail: the response is dropped; the server keeps the built document for the player's request.
fetch(url).catch(() => undefined);
}