Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Created July 5, 2022 07:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusukebe/232f14efae731a6ed2afa12ae370b0d0 to your computer and use it in GitHub Desktop.
Save yusukebe/232f14efae731a6ed2afa12ae370b0d0 to your computer and use it in GitHub Desktop.
import React, { Suspense } from 'react'
import { renderToReadableStream } from 'react-dom/server'
import { Hono } from 'hono'
const DELAY = 2000
let done = false
const Component: React.FC = () => {
if (done) {
done = false
return (
<p>
<b>Hello World!!</b>
</p>
)
}
throw new Promise((resolve) =>
setTimeout(() => {
done = true
resolve(true)
}, DELAY)
)
}
const app = new Hono()
app.get('/', async (c) => {
const stream = await renderToReadableStream(
<html>
<body>
<Suspense fallback={<p>Loading...</p>}>
<Component />
</Suspense>
</body>
</html>
)
return new Response(stream, {
headers: {
'X-Content-Type-Options': 'nosniff',
'Content-Type': 'text/html',
},
})
})
export default app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment