Created
July 5, 2022 07:39
-
-
Save yusukebe/232f14efae731a6ed2afa12ae370b0d0 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 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