|
| 1 | +import { renderToString } from "react-dom/server"; |
| 2 | +import React from "react"; |
| 3 | +console.log(process.env); |
| 4 | +type SnippetKey = "bash" | "python" | "tsx"; |
| 5 | + |
| 6 | +const BASH_SNIPPET = `$ elide app.py`; |
| 7 | + |
| 8 | +const PYTHON_SNIPPET = ` |
| 9 | +from elide import Flask, react, request |
| 10 | +
|
| 11 | +# create and configure an app |
| 12 | +app = Flask(__name__) |
| 13 | +home = react("index.tsx") |
| 14 | +
|
| 15 | +# basic app routing |
| 16 | +@app.route("/") |
| 17 | +def hello_world(): |
| 18 | + user_agent = request.headers["User-Agent"] |
| 19 | + return home(user_agent=user_agent) |
| 20 | +`.trim(); |
| 21 | + |
| 22 | +const REACT_SNIPPET = ` |
| 23 | +export default async function render(props): Promise<string> { |
| 24 | + return renderToString( |
| 25 | + <App |
| 26 | + nowISO={new Date().toISOString()} |
| 27 | + ua={props.user_agent ?? ""} |
| 28 | + reqId={Date.now()} |
| 29 | + pid={process.pid} |
| 30 | + />, |
| 31 | + ); |
| 32 | +} |
| 33 | +`.trim(); |
| 34 | + |
| 35 | +const SNIPPETS: Array<{ |
| 36 | + key: SnippetKey; |
| 37 | + title: string; |
| 38 | + subtitle: string; |
| 39 | + language: string; |
| 40 | + code: string; |
| 41 | +}> = [ |
| 42 | + { |
| 43 | + key: "bash", |
| 44 | + title: "Run in one line", |
| 45 | + subtitle: |
| 46 | + "Launch the server without any additional steps: no virtual environments, no special commands", |
| 47 | + language: "bash", |
| 48 | + code: BASH_SNIPPET, |
| 49 | + }, |
| 50 | + { |
| 51 | + key: "python", |
| 52 | + title: "Use your existing Python server code", |
| 53 | + subtitle: "Serve React markup from the Flask apps you already have", |
| 54 | + language: "python", |
| 55 | + code: PYTHON_SNIPPET, |
| 56 | + }, |
| 57 | + { |
| 58 | + key: "tsx", |
| 59 | + title: "Tap into the React ecosystem", |
| 60 | + subtitle: "Render React components server-side with Elide", |
| 61 | + language: "typescript", |
| 62 | + code: REACT_SNIPPET, |
| 63 | + }, |
| 64 | +]; |
| 65 | + |
| 66 | +function App(props: { |
| 67 | + nowISO: string; |
| 68 | + ua: string; |
| 69 | + reqId: string; |
| 70 | + pid: number; |
| 71 | +}) { |
| 72 | + return ( |
| 73 | + <html lang="en"> |
| 74 | + <head> |
| 75 | + <meta charSet="utf-8" /> |
| 76 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 77 | + <title>Pure React SSR · No Client JS</title> |
| 78 | + <link rel="preconnect" href="https://fonts.googleapis.com" /> |
| 79 | + <link |
| 80 | + rel="preconnect" |
| 81 | + href="https://fonts.gstatic.com" |
| 82 | + crossOrigin="" |
| 83 | + /> |
| 84 | + <link |
| 85 | + href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;600&family=Roboto:wght@400;500;700&family=Roboto+Condensed:wght@700&display=swap" |
| 86 | + rel="stylesheet" |
| 87 | + /> |
| 88 | + <link rel="stylesheet" href="/static/styles.css" /> |
| 89 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js"></script> |
| 90 | + <script>hljs.highlightAll();</script> |
| 91 | + </head> |
| 92 | + <body> |
| 93 | + <header> |
| 94 | + <a className="hero" href="/" aria-label="Home"> |
| 95 | + <img |
| 96 | + className="logo" |
| 97 | + alt="Elide logo" |
| 98 | + src="/static/elide-logo.svg" |
| 99 | + /> |
| 100 | + <span>Powered by Elide</span> |
| 101 | + </a> |
| 102 | + <h1>React SSR + Flask delivered by Elide</h1> |
| 103 | + <p className="tagline"> |
| 104 | + This app renders entirely on the server, it is written using Flask |
| 105 | + and is served by Elide's new polyglot HTTP engine |
| 106 | + </p> |
| 107 | + </header> |
| 108 | + |
| 109 | + <main> |
| 110 | + <section className="grid"> |
| 111 | + <div className="card"> |
| 112 | + <h2>Request information</h2> |
| 113 | + <p className="lead"> |
| 114 | + This section is generated on the server for each request |
| 115 | + </p> |
| 116 | + <ul> |
| 117 | + <li> |
| 118 | + Render time: <strong>{props.nowISO}</strong> |
| 119 | + </li> |
| 120 | + <li> |
| 121 | + Request ID: <strong>{props.reqId}</strong> |
| 122 | + </li> |
| 123 | + <li> |
| 124 | + Process PID: <strong>{props.pid}</strong> |
| 125 | + </li> |
| 126 | + <li> |
| 127 | + User-Agent: <strong>{props.ua || "(unknown)"}</strong> |
| 128 | + </li> |
| 129 | + </ul> |
| 130 | + </div> |
| 131 | + </section> |
| 132 | + |
| 133 | + <section className="code-sections"> |
| 134 | + {SNIPPETS.map((snippet) => ( |
| 135 | + <div className="code-card" key={snippet.key}> |
| 136 | + <h3>{snippet.title}</h3> |
| 137 | + <p className="snippet-subtitle">{snippet.subtitle}</p> |
| 138 | + <pre> |
| 139 | + <code className={`language-${snippet.language}`}> |
| 140 | + {snippet.code} |
| 141 | + </code> |
| 142 | + </pre> |
| 143 | + </div> |
| 144 | + ))} |
| 145 | + </section> |
| 146 | + </main> |
| 147 | + |
| 148 | + <footer> |
| 149 | + Built with <span className="kbd">React 18</span> and Flask on Elide. |
| 150 | + </footer> |
| 151 | + </body> |
| 152 | + </html> |
| 153 | + ); |
| 154 | +} |
| 155 | + |
| 156 | +export default async function render(props): Promise<string> { |
| 157 | + return renderToString( |
| 158 | + <App |
| 159 | + nowISO={new Date().toISOString()} |
| 160 | + ua={props.user_agent ?? ""} |
| 161 | + reqId={Date.now()} |
| 162 | + pid={process.pid} |
| 163 | + />, |
| 164 | + ); |
| 165 | +} |
0 commit comments