feat(examples)+test(e2e): streaming SSR demo, proven progressive in Chromium
examples/streaming is a runnable Node server piping toStream() through Readable.fromWeb. The e2e server gains /stream, and two tests prove real streaming: shell bytes arrive over HTTP before the async hole resolves, and Chromium builds the shell DOM mid-stream, then grows the slow section into the same document without navigation. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014NgfSjHE11oFpoSnLVKLXd
This commit is contained in:
co-authored by
Claude Fable 5
parent
22c5045c25
commit
d22f996596
@@ -0,0 +1,35 @@
|
||||
// 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>`
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Minimal streaming SSR server on plain Node http.
|
||||
// node examples/streaming/server.js
|
||||
// The same ReadableStream plugs into web-standard servers as
|
||||
// `new Response(app.toStream('/'))` — Readable.fromWeb is just the
|
||||
// Node http bridge.
|
||||
|
||||
import { createServer } from 'node:http'
|
||||
import { Readable } from 'node:stream'
|
||||
|
||||
import createApp from './app.js'
|
||||
|
||||
const PORT = process.env.PORT || 8080
|
||||
|
||||
createServer((req, res) => {
|
||||
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' })
|
||||
res.write('<!doctype html>\n<html lang="en">\n<head><meta charset="utf-8"><title>choo streams</title></head>\n')
|
||||
|
||||
const body = Readable.fromWeb(createApp().toStream('/'))
|
||||
body.pipe(res, { end: false })
|
||||
body.on('end', () => res.end('\n</html>'))
|
||||
body.on('error', (err) => {
|
||||
console.error(err)
|
||||
res.destroy()
|
||||
})
|
||||
}).listen(PORT, () => {
|
||||
console.log(`streaming on http://localhost:${PORT} — watch the slow section arrive`)
|
||||
})
|
||||
Reference in New Issue
Block a user