Last active
April 11, 2024 05:35
-
-
Save pazguille/a2e3792c1901e12e67453ccb19cc0da6 to your computer and use it in GitHub Desktop.
Deno + RSC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // deno run --allow-net rsc.jsx | |
| // deno run --allow-net https://gist.github.com/pazguille/a2e3792c1901e12e67453ccb19cc0da6/raw/752200e809993fd2176381b7b09d8308dca6d069/rsc.jsx | |
| import { serve } from 'https://deno.land/std/http/server.ts'; | |
| import React, { Suspense } from "https://esm.sh/[email protected]"; | |
| import ReactDOMServer from 'https://esm.sh/[email protected]/server'; | |
| function Hello({ name }) { | |
| return <p>Hello {name}!</p>; | |
| } | |
| async function Games() { | |
| const data = await fetch('https://api.xstoregames.com/api/games?list=new&skipitems=0').then(r => r.json()); | |
| await new Promise(r => setTimeout(r, 2000)); | |
| return ( | |
| <ul> | |
| {data.map(game => (<li>{game.title}</li>))} | |
| </ul> | |
| ); | |
| } | |
| function Home() { | |
| return ( | |
| <> | |
| <h1>Deno + RSC</h1> | |
| <Hello name="Guille" /> | |
| <Suspense fallback={<p>Loading...</p>}> | |
| <Games /> | |
| </Suspense> | |
| </> | |
| ); | |
| } | |
| function App({ children }) { | |
| return ( | |
| <html> | |
| <head> | |
| <title>Deno + RSC</title> | |
| </head> | |
| <body> | |
| {children} | |
| </body> | |
| </html> | |
| ); | |
| } | |
| async function handler(req) { | |
| if (req.url.includes('favicon')) { | |
| return new Response(null, { | |
| status: 204, | |
| }); | |
| }; | |
| const htmlStream = await ReactDOMServer.renderToReadableStream( | |
| <App> | |
| <Home /> | |
| </App> | |
| ); | |
| return new Response(htmlStream, { | |
| headers: { | |
| "content-type": "text/html", | |
| }, | |
| }); | |
| } | |
| serve(handler, { port: 3030 }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment