Files
buuh/examples/streaming/app.js
T

36 lines
1.1 KiB
JavaScript
Raw Normal View History

// 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.
import choo from '@choojs/core'
import html from '@choojs/html'
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>`
}