Unverified Commit 89996f10 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

refactor: handle null RenderResult responses gracefully (#81895)

### What?
- Removed unused `toUnchunkedBuffer` method from RenderResult
- Changed null response handling to return empty values instead of
throwing errors

### Why?
The codebase now provides `RenderResult.EMPTY` for null responses,
making it a valid state rather than an error condition. This change
ensures consistent behavior across the rendering pipeline when dealing
with empty responses.

### How?
- Modified `toUnchunkedString()` to return empty string for null
responses
- Updated `coerce()` to return empty array for null responses  
- Fixed `chainStreams()` to return an empty stream when no streams are
provided
- Removed dead code that was no longer used

This refactor improves error handling and simplifies the stream
processing logic by treating empty/null responses as valid states.
parent 2632a459
...@@ -6,7 +6,6 @@ import { ...@@ -6,7 +6,6 @@ import {
chainStreams, chainStreams,
streamFromBuffer, streamFromBuffer,
streamFromString, streamFromString,
streamToBuffer,
streamToString, streamToString,
} from './stream-utils/node-web-streams-helper' } from './stream-utils/node-web-streams-helper'
import { isAbortError, pipeToNodeResponse } from './pipe-readable' import { isAbortError, pipeToNodeResponse } from './pipe-readable'
...@@ -167,26 +166,6 @@ export default class RenderResult< ...@@ -167,26 +166,6 @@ export default class RenderResult<
return typeof this.response !== 'string' return typeof this.response !== 'string'
} }
public toUnchunkedBuffer(stream?: false): Buffer
public toUnchunkedBuffer(stream: true): Promise<Buffer>
public toUnchunkedBuffer(stream = false): Promise<Buffer> | Buffer {
if (this.response === null) {
throw new InvariantError('null responses cannot be unchunked')
}
if (typeof this.response !== 'string') {
if (!stream) {
throw new InvariantError(
'dynamic responses cannot be unchunked. This is a bug in Next.js'
)
}
return streamToBuffer(this.readable)
}
return Buffer.from(this.response)
}
/** /**
* Returns the response if it is a string. If the page was dynamic, this will * Returns the response if it is a string. If the page was dynamic, this will
* return a promise if the `stream` option is true, or it will throw an error. * return a promise if the `stream` option is true, or it will throw an error.
...@@ -198,7 +177,9 @@ export default class RenderResult< ...@@ -198,7 +177,9 @@ export default class RenderResult<
public toUnchunkedString(stream: true): Promise<string> public toUnchunkedString(stream: true): Promise<string>
public toUnchunkedString(stream = false): Promise<string> | string { public toUnchunkedString(stream = false): Promise<string> | string {
if (this.response === null) { if (this.response === null) {
throw new InvariantError('null responses cannot be unchunked') // If the response is null, return an empty string. This behavior is
// intentional as we're now providing the `RenderResult.EMPTY` value.
return ''
} }
if (typeof this.response !== 'string') { if (typeof this.response !== 'string') {
...@@ -252,7 +233,9 @@ export default class RenderResult< ...@@ -252,7 +233,9 @@ export default class RenderResult<
*/ */
private coerce(): ReadableStream<Uint8Array>[] { private coerce(): ReadableStream<Uint8Array>[] {
if (this.response === null) { if (this.response === null) {
throw new Error('Invariant: response is null. This is a bug in Next.js') // If the response is null, return an empty stream. This behavior is
// intentional as we're now providing the `RenderResult.EMPTY` value.
return []
} }
if (typeof this.response === 'string') { if (typeof this.response === 'string') {
......
...@@ -10,7 +10,6 @@ import { ...@@ -10,7 +10,6 @@ import {
} from './uint8array-helpers' } from './uint8array-helpers'
import { MISSING_ROOT_TAGS_ERROR } from '../../shared/lib/errors/constants' import { MISSING_ROOT_TAGS_ERROR } from '../../shared/lib/errors/constants'
import { insertBuildIdComment } from '../../shared/lib/segment-cache/output-export-prefetch-encoding' import { insertBuildIdComment } from '../../shared/lib/segment-cache/output-export-prefetch-encoding'
import { InvariantError } from '../../shared/lib/invariant-error'
function voidCatch() { function voidCatch() {
// this catcher is designed to be used with pipeTo where we expect the underlying // this catcher is designed to be used with pipeTo where we expect the underlying
...@@ -30,10 +29,14 @@ const encoder = new TextEncoder() ...@@ -30,10 +29,14 @@ const encoder = new TextEncoder()
export function chainStreams<T>( export function chainStreams<T>(
...streams: ReadableStream<T>[] ...streams: ReadableStream<T>[]
): ReadableStream<T> { ): ReadableStream<T> {
// We could encode this invariant in the arguments but current uses of this function pass // If we have no streams, return an empty stream. This behavior is
// use spread so it would be missed by // intentional as we're now providing the `RenderResult.EMPTY` value.
if (streams.length === 0) { if (streams.length === 0) {
throw new InvariantError('chainStreams requires at least one stream') return new ReadableStream<T>({
start(controller) {
controller.close()
},
})
} }
// If we only have 1 stream we fast path it by returning just this stream // If we only have 1 stream we fast path it by returning just this stream
......
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