Unverified Commit 0ecbcc6d authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix unnecessary flight render passes (#38133)

Currently `renderToReadableStream` will be called whenever the component is re-rendered, but the result should actually be cached (per request).

Thanks to @sebmarkbage for pointing out.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 42e33655
...@@ -78,7 +78,7 @@ export default async function edgeSSRLoader(this: any) { ...@@ -78,7 +78,7 @@ export default async function edgeSSRLoader(this: any) {
buildId: ${JSON.stringify(buildId)}, buildId: ${JSON.stringify(buildId)},
}) })
export default function rscMiddleware(opts) { export default function(opts) {
return adapter({ return adapter({
...opts, ...opts,
handler: render handler: render
......
...@@ -180,13 +180,20 @@ function createServerComponentRenderer( ...@@ -180,13 +180,20 @@ function createServerComponentRenderer(
globalThis.__next_chunk_load__ = () => Promise.resolve() globalThis.__next_chunk_load__ = () => Promise.resolve()
} }
const writable = transformStream.writable let RSCStream: ReadableStream<Uint8Array>
const ServerComponentWrapper = (props: any) => { const createRSCStream = () => {
const reqStream: ReadableStream<Uint8Array> = renderToReadableStream( if (!RSCStream) {
<ComponentToRender {...props} />, RSCStream = renderToReadableStream(
serverComponentManifest <ComponentToRender />,
) serverComponentManifest
)
}
return RSCStream
}
const writable = transformStream.writable
const ServerComponentWrapper = () => {
const reqStream = createRSCStream()
const response = useFlightResponse( const response = useFlightResponse(
writable, writable,
cachePrefix, cachePrefix,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment