2026-09-08 18:44:52 +02:00
|
|
|
// Streaming SSR demo: the shell flushes immediately, the slow section
|
|
|
|
|
// streams in when its promise resolves. Run it:
|
|
|
|
|
// node examples/streaming/server.js
|
|
|
|
|
//
|
|
|
|
|
// This page is server-rendered only (no hydration script): async holes
|
|
|
|
|
// stream on the server, while client-side views must be synchronous.
|
|
|
|
|
// Serializing streamed state for hydration is bankai v10 territory.
|
|
|
|
|
|
2026-09-08 19:55:20 +02:00
|
|
|
import choo from '@uhhm/buuh'
|
|
|
|
|
import html from '@uhhm/buuh-html'
|
2026-09-08 18:44:52 +02:00
|
|
|
|
|
|
|
|
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
|
|
|
|
|
|
|
|
export default function createApp ({ delay = 500 } = {}) {
|
|
|
|
|
const app = choo()
|
|
|
|
|
app.use((state) => { state.delay = delay })
|
|
|
|
|
app.route('/', mainView)
|
|
|
|
|
app.route('*', mainView)
|
|
|
|
|
return app
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mainView (state, emit) {
|
|
|
|
|
return html`
|
|
|
|
|
<body>
|
|
|
|
|
<h1>choo streams</h1>
|
|
|
|
|
<p>This shell was flushed before the slow part finished.</p>
|
|
|
|
|
${slowSection(state)}
|
|
|
|
|
</body>
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function slowSection (state) {
|
|
|
|
|
await wait(state.delay)
|
|
|
|
|
return html`<section id="slow">…and this arrived ${state.delay}ms later, same response.</section>`
|
|
|
|
|
}
|