From 72d0f1b64af748028f710edb89014a53ce971ab3 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 19 Jul 2026 10:02:44 +0000 Subject: [PATCH] fix: set Content-Type header when preview server serves HTML The preview-server middleware in `configurePreviewServer` responded with `res.end(html)` without a `Content-Type` header, for both matched entry files and the SPA fallback. Browsers usually sniff it as HTML, but some environments and tooling won't. Set an explicit `Content-Type: text/html; charset=utf-8` before ending the response. Also collapse the two separate lookup loops (candidate files, then the `index.html`/`index.htm` SPA fallback) into a single deduplicated candidate list. Closes #142 Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_018H1uPDiSpxrHtQvyAxUEVC --- packages/static/src/plugin/server.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/packages/static/src/plugin/server.ts b/packages/static/src/plugin/server.ts index 4689787..3248c1e 100644 --- a/packages/static/src/plugin/server.ts +++ b/packages/static/src/plugin/server.ts @@ -54,26 +54,22 @@ export const serverPlugin = (): Plugin => { if (req.headers.accept?.includes("text/html")) { const urlPath = new URL(req.url!, `http://${req.headers.host}`) .pathname; - const candidates = urlPathToFileCandidates(urlPath); + // Entry files matching the URL path, followed by the SPA + // fallback (index.html / index.htm) for unmatched routes. + const candidates = [ + ...new Set([ + ...urlPathToFileCandidates(urlPath), + "index.html", + "index.htm", + ]), + ]; for (const candidate of candidates) { try { const html = await readFile( path.join(resolvedOutDir, candidate), "utf-8", ); - res.end(html); - return; - } catch { - // Try next candidate - } - } - // SPA fallback: try serving index.html or index.htm for unmatched routes - for (const indexFile of ["index.html", "index.htm"]) { - try { - const html = await readFile( - path.join(resolvedOutDir, indexFile), - "utf-8", - ); + res.setHeader("Content-Type", "text/html; charset=utf-8"); res.end(html); return; } catch {