From 3f96d8a6e75b616cc2308e00300a903acfc0fc09 Mon Sep 17 00:00:00 2001 From: MythEclipse Date: Wed, 10 Jun 2026 21:59:09 +0700 Subject: [PATCH] fix: relay requests to / with x-relay-target instead of showing index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a GET request hits / with an x-relay-target header, it should relay the request — not display the index page. Fixed in all three entry points (src/index.ts, api/relay.ts, src/worker.ts). Co-Authored-By: Claude Fable 5 --- api/relay.ts | 10 ++++++++-- src/index.ts | 3 ++- src/worker.ts | 10 ++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/api/relay.ts b/api/relay.ts index aa42fdd..1a47f58 100644 --- a/api/relay.ts +++ b/api/relay.ts @@ -362,10 +362,16 @@ export default { async fetch(req: Request): Promise { const url = new URL(req.url); - // Static routes + // Static routes — show index only when no relay target is requested if (url.pathname === "/health") return handleHealth(); if (url.pathname === "/docs") return handleDocs(); - if (url.pathname === "/" && req.method === "GET") return handleIndex(); + if ( + url.pathname === "/" && + req.method === "GET" && + !req.headers.get("x-relay-target") + ) { + return handleIndex(); + } // WebSocket upgrade — not supported in Vercel Functions if ( diff --git a/src/index.ts b/src/index.ts index 61c5ed7..7d69782 100644 --- a/src/index.ts +++ b/src/index.ts @@ -433,7 +433,8 @@ const server: Server = Bun.serve({ // Static routes if (url.pathname === "/health") return handleHealth(); if (url.pathname === "/docs") return handleDocs(); - if (url.pathname === "/" && req.method === "GET") return handleIndex(); + if (url.pathname === "/" && req.method === "GET" && !req.headers.get("x-relay-target")) + return handleIndex(); // WebSocket upgrade check — if the target is ws:// or wss://, // attempt to upgrade and relay. This must happen before the diff --git a/src/worker.ts b/src/worker.ts index 79cf4e7..e32c9aa 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -358,10 +358,16 @@ export default { async fetch(req: Request, env: Env): Promise { const url = new URL(req.url); - // Static routes + // Static routes — show index only when no relay target is requested if (url.pathname === "/health") return handleHealth(); if (url.pathname === "/docs") return handleDocs(); - if (url.pathname === "/" && req.method === "GET") return handleIndex(); + if ( + url.pathname === "/" && + req.method === "GET" && + !req.headers.get("x-relay-target") + ) { + return handleIndex(); + } // WebSocket upgrade — not fully supported in this handler if (