The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 66 lines 1.97 KB
Raw
1 import { StaticRouter } from "@solidjs/router";
2 import { renderToStringAsync, generateHydrationScript } from "solid-js/web";
3 import App from "./app";
4 import { routes } from "./routes";
5
6 const FALLBACK_HTML = `<!DOCTYPE html>
7 <html lang="en">
8 <head>
9 <meta charset="utf-8" />
10 <meta name="viewport" content="width=device-width, initial-scale=1" />
11 <title>Solid App</title>
12 <!--ssr-head-->
13 </head>
14 <body>
15 <div id="root"><!--ssr-outlet--></div>
16 </body>
17 </html>`;
18
19 async function getTemplate(env: { ASSETS: Fetcher }, origin: string): Promise<string> {
20 try {
21 const res = await env.ASSETS.fetch(new Request(origin + "/index.html"));
22 if (res.ok) {
23 const text = await res.text();
24 if (text.length > 0) return text;
25 }
26 } catch {}
27 return FALLBACK_HTML;
28 }
29
30 export default {
31 async fetch(request: Request, env: { ASSETS: Fetcher }): Promise<Response> {
32 try {
33 const url = new URL(request.url);
34 const pathname = url.pathname;
35
36 // Serve static assets via the assets binding
37 if (pathname.startsWith("/assets/") || pathname.endsWith(".ico")) {
38 return env.ASSETS.fetch(request);
39 }
40
41 // Get the HTML template (from assets if available, fallback to bare HTML)
42 const template = await getTemplate(env, url.origin);
43
44 // Render the SolidJS app to HTML on the server
45 const appHtml = await renderToStringAsync(() => (
46 <StaticRouter url={pathname} root={(props) => <App>{props.children}</App>}>
47 {routes}
48 </StaticRouter>
49 ));
50
51 // Inject the server-rendered HTML and hydration script
52 const html = template
53 .replace("<!--ssr-head-->", generateHydrationScript())
54 .replace("<!--ssr-outlet-->", appHtml);
55
56 return new Response(html, {
57 headers: { "Content-Type": "text/html; charset=utf-8" },
58 });
59 } catch (e: any) {
60 return new Response(`SSR Error: ${e.message}\n\n${e.stack}`, {
61 status: 500,
62 headers: { "Content-Type": "text/plain" },
63 });
64 }
65 },
66 };