Created
November 7, 2023 02:19
-
-
Save yusukebe/ff12de95b001c2dd6896f4b5a7367dbc 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 { Suspense, renderToReadableStream } from 'hono/jsx/streaming' | |
import { Todo } from './types' | |
const app = new Hono() | |
const fetchData = async () => { | |
const res = await fetch(`https://jsonplaceholder.typicode.com/todos/1`) | |
return res.json<Todo>() | |
} | |
const Component = async () => { | |
const data = await fetchData() | |
return <div>{data.title}</div> | |
} | |
app.get('/', (c) => { | |
return c.body( | |
renderToReadableStream( | |
<html> | |
<body> | |
<h1>SSR Streaming</h1> | |
<Suspense fallback={<div>loading...</div>}> | |
<Component /> | |
</Suspense> | |
</body> | |
</html> | |
), | |
{ | |
headers: { | |
'Content-Type': 'text/html; charset=UTF-8', | |
'Transfer-Encoding': 'chunked' | |
} | |
} | |
) | |
}) | |
export default app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment