Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Created November 7, 2023 02:19
Show Gist options
  • Save yusukebe/ff12de95b001c2dd6896f4b5a7367dbc to your computer and use it in GitHub Desktop.
Save yusukebe/ff12de95b001c2dd6896f4b5a7367dbc to your computer and use it in GitHub Desktop.
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