| 1 | import { createRequestHandler } from "react-router"; |
| 2 | |
| 3 | const requestHandler = createRequestHandler( |
| 4 | () => import("virtual:react-router/server-build"), |
| 5 | import.meta.env.MODE, |
| 6 | ); |
| 7 | |
| 8 | const GIT_PATH = /\/(info\/refs|git-upload-pack|git-receive-pack)$/; |
| 9 | const DOCS_HOST = "docs.g1t.sh"; |
| 10 | const SITE = "https://g1t.sh"; |
| 11 | |
| 12 | /** |
| 13 | * docs.g1t.sh serves the site's /docs pages from its root: /concepts there |
| 14 | * is /docs/concepts here. Anything on that hostname that is not |
| 15 | * documentation belongs to the main site. |
| 16 | */ |
| 17 | function docsRequest(request: Request, url: URL): Request | Response { |
| 18 | const { pathname } = url; |
| 19 | if (pathname === "/docs" || pathname.startsWith("/docs/")) return request; |
| 20 | // React Router's data requests for in-site navigation. |
| 21 | if (pathname.startsWith("/__manifest") || pathname.endsWith(".data")) return request; |
| 22 | const first = pathname.split("/")[1]; |
| 23 | if (DOCS_PAGES.has(first)) { |
| 24 | url.pathname = "/docs" + (pathname === "/" ? "" : pathname); |
| 25 | return new Request(url, request); |
| 26 | } |
| 27 | return Response.redirect(SITE + pathname + url.search, 302); |
| 28 | } |
| 29 | |
| 30 | /** First path segments that are documentation; "" is the docs home page. */ |
| 31 | const DOCS_PAGES = new Set([ |
| 32 | "", |
| 33 | "concepts", |
| 34 | "authentication", |
| 35 | "git", |
| 36 | "g1t-agents", |
| 37 | "agents", |
| 38 | "api", |
| 39 | ]); |
| 40 | |
| 41 | export default { |
| 42 | async fetch(request, env) { |
| 43 | const url = new URL(request.url); |
| 44 | // Git over HTTPS shares this hostname but belongs to the repos service. |
| 45 | if (GIT_PATH.test(url.pathname)) { |
| 46 | return env.REPOS.fetch(request); |
| 47 | } |
| 48 | if (url.hostname === DOCS_HOST) { |
| 49 | const rewritten = docsRequest(request, url); |
| 50 | if (rewritten instanceof Response) return rewritten; |
| 51 | return requestHandler(rewritten); |
| 52 | } |
| 53 | return requestHandler(request); |
| 54 | }, |
| 55 | } satisfies ExportedHandler<Env>; |