Skip to content

Commit

Permalink
Add initial ResponsePayload support (#26938)
Browse files Browse the repository at this point in the history
Refactors the internals of `next-server` to use `ResponsePayload` instead of `string | null` and manual `sendPayload` calls. This is the first step toward streaming support.

I split `renderToResponseWithComponents` into a separate `renderToResponseWithComponentsInternal` function for ease of review: GitHub's diff rendering was highly misleading, making it seem as though more of the function had changed. The separate function just makes the actual change clearer: we split `renderToHTMLWithComponents` into two promises; one that represents the actual render result, and one that represents all of the work (including background work for e.g. revalidation) that needs to be done as part of generating the result.

These changes make it easier to bubble up a `ResponsePayload`, instead of sometimes calling `sendPayload` out-of-band, centralizing all payload handling in `sendResponse` and eventually a similar function for public APIs that return a string. This centralization will make it much easier to handle a response that needs to be streamed, which is coming soon in another PR.
  • Loading branch information
devknoll authored Jul 6, 2021
1 parent 402f036 commit 2795b79
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 68 deletions.
7 changes: 4 additions & 3 deletions packages/next/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Server, {
WrappedBuildError,
ServerConstructor,
FindComponentsResult,
ResponsePayload,
} from '../next-server'
import { normalizePagePath } from '../normalize-page-path'
import Router, { Params, route } from '../router'
Expand Down Expand Up @@ -636,14 +637,14 @@ export default class DevServer extends Server {
return await loadDefaultErrorComponents(this.distDir)
}

sendHTML(
sendResponse(
req: IncomingMessage,
res: ServerResponse,
html: string
response: ResponsePayload
): Promise<void> {
// In dev, we should not cache pages for any reason.
res.setHeader('Cache-Control', 'no-store, must-revalidate')
return super.sendHTML(req, res, html)
return super.sendResponse(req, res, response)
}

protected setImmutableAssetCacheControl(res: ServerResponse): void {
Expand Down
Loading

0 comments on commit 2795b79

Please sign in to comment.