rsnext/packages/next/server/render-result.ts
Gerald Monaco 944c734d04
Add unstable_useFlushEffects hook (#34117)
Implements https://github.com/vercel/next.js/issues/30997 with some minor tweaks to the design:

* The hook is moved to Client Components (e.g. `pages/_app` instead of `pages/_document`). This was a silly oversight in the original design: the hook needs to be called during server prerendering.

* `useFlushEffects` instead of `useFlushEffect` as there isn't a particularly safe way to implement the singular semantics as a Client Component hook given the current implementation of server rendering.

---

Fixes #30997
2022-02-18 00:18:28 +00:00

66 lines
1.5 KiB
TypeScript

import type { ServerResponse } from 'http'
export default class RenderResult {
_result: string | ReadableStream<Uint8Array>
constructor(response: string | ReadableStream<Uint8Array>) {
this._result = response
}
toUnchunkedString(): string {
if (typeof this._result !== 'string') {
throw new Error(
'invariant: dynamic responses cannot be unchunked. This is a bug in Next.js'
)
}
return this._result
}
pipe(res: ServerResponse): Promise<void> {
if (typeof this._result === 'string') {
throw new Error(
'invariant: static responses cannot be piped. This is a bug in Next.js'
)
}
const response = this._result
const flush =
typeof (res as any).flush === 'function'
? () => (res as any).flush()
: () => {}
return (async () => {
const reader = response.getReader()
let fatalError = false
try {
while (true) {
const { done, value } = await reader.read()
if (done) {
res.end()
return
}
fatalError = true
res.write(value)
flush()
}
} catch (err) {
if (fatalError) {
res.destroy(err as any)
}
throw err
}
})()
}
isDynamic(): boolean {
return typeof this._result !== 'string'
}
static fromStatic(value: string): RenderResult {
return new RenderResult(value)
}
static empty = RenderResult.fromStatic('')
}