Created
November 5, 2023 17:50
-
-
Save yusukebe/00175b480997c4cd2eff4e543af2c865 to your computer and use it in GitHub Desktop.
This file contains 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
import { Hono } from 'hono' | |
import { use, Suspense, renderToReadableStream } from 'hono/jsx/streaming' | |
import type { Shop } from './types' | |
const app = new Hono() | |
const fetchData = async (): Promise<{ shop: Shop }> => { | |
const res = await fetch('https://ramen-api.dev/shops/yoshimuraya') | |
return res.json() | |
} | |
const AsyncComponent = () => { | |
const data = use(fetchData()) | |
return <p>I like {data.shop.name} 🍜</p> | |
} | |
app.get('/', async (c) => { | |
const stream = renderToReadableStream( | |
<html> | |
<body> | |
<h1>SSR Streaming</h1> | |
<Suspense fallback={<p>loading...</p>}> | |
<AsyncComponent /> | |
</Suspense> | |
</body> | |
</html> | |
) | |
return c.body(stream, { | |
headers: { | |
'Transfer-Encoding': 'chunked', | |
'Content-Type': 'text/html; charset=UTF-8' | |
} | |
}) | |
}) | |
export default app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment