Fix infinite redirect on /studio by serving index.html directly

[...path] catches /studio itself (empty rest param), causing a redirect
loop. Now reads and returns index.html content for all /studio/* paths;
static assets are still served before this route fires.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Calum Muir 2026-06-07 12:27:21 +00:00
parent 9ca50a1da8
commit 0596e6db47

View file

@ -1,7 +1,21 @@
import type { APIRoute } from "astro"; import type { APIRoute } from "astro";
import { readFileSync, existsSync } from "fs";
import { join } from "path";
// Redirect deep studio links (e.g. /studio/desk/post) back to /studio so the // Serve the studio SPA for all /studio/* paths (including /studio itself).
// SPA can boot and handle its own routing. Static assets under /studio/assets/* // Static assets under /studio/assets/* are served before this route fires.
// are served before this route ever fires. export const GET: APIRoute = () => {
export const GET: APIRoute = () => const indexPath = join(process.cwd(), "dist/client/studio/index.html");
new Response(null, { status: 302, headers: { Location: "/studio" } });
if (!existsSync(indexPath)) {
return new Response(
"<p>Sanity Studio has not been built. Check the Docker build logs.</p>",
{ status: 503, headers: { "Content-Type": "text/html" } }
);
}
return new Response(readFileSync(indexPath, "utf-8"), {
status: 200,
headers: { "Content-Type": "text/html; charset=utf-8" },
});
};