Skip to content

Commit a84672e

Browse files
authored
Use react dom server node api to detect react root is enabled (#36749)
x-ref: #36552 (comment) x-ref: preactjs/next-plugin-preact#59 `preact/compat` doesn't have `/server.browser` exports, to make it work with latest of next.js: * use `react-dom/server` to detect if it could opt-in streaming rendering by checking react 18 `renderToPipeableStream` API in short time fix. In long term `preact/compat`should support `/server.browser` that same with react 17. * Also filed a PR to `next-plugin-preact` to skip chunk-prepending to pages in edge compiler
1 parent 51d962d commit a84672e

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

packages/next/bin/next.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ const args = arg(
4343
)
4444

4545
// Detect if react-dom is enabled streaming rendering mode
46-
const shouldUseReactRoot = !!require('react-dom/server.browser')
47-
.renderToReadableStream
46+
const shouldUseReactRoot = !!require('react-dom/server').renderToPipeableStream
4847

4948
// Version is inlined into the file using taskr build pipeline
5049
if (args['--version']) {

packages/next/server/next.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ function createServer(options: NextServerOptions): NextServer {
185185

186186
// Make sure env of custom server is overridden.
187187
// Use dynamic require to make sure it's executed in it's own context.
188-
const ReactDOMServer = require('react-dom/server.browser')
189-
const shouldUseReactRoot = !!ReactDOMServer.renderToReadableStream
188+
const ReactDOMServer = require('react-dom/server')
189+
const shouldUseReactRoot = !!ReactDOMServer.renderToPipeableStream
190190
if (shouldUseReactRoot) {
191191
;(process.env as any).__NEXT_REACT_ROOT = 'true'
192192
}

packages/next/server/view-render.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import {
1717
continueFromInitialStream,
1818
} from './node-web-streams-helper'
1919
import { FlushEffectsContext } from '../shared/lib/flush-effects'
20-
// @ts-ignore react-dom/client exists when using React 18
21-
import ReactDOMServer from 'react-dom/server.browser'
2220
import { isDynamicRoute } from '../shared/lib/router/utils'
2321
import { tryGetPreviewData } from './api-utils/node'
2422
import DefaultRootLayout from '../lib/views-layout'
2523

24+
const ReactDOMServer = process.env.__NEXT_REACT_ROOT
25+
? require('react-dom/server.browser')
26+
: require('react-dom/server')
27+
2628
export type RenderOptsPartial = {
2729
err?: Error | null
2830
dev?: boolean

0 commit comments

Comments
 (0)