Unverified Commit a84672e6 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Use react dom server node api to detect react root is enabled (#36749)

x-ref: https://github.com/vercel/next.js/pull/36552#issuecomment-1120128946
x-ref: https://github.com/preactjs/next-plugin-preact/pull/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
parent 51d962d6
...@@ -43,8 +43,7 @@ const args = arg( ...@@ -43,8 +43,7 @@ const args = arg(
) )
// Detect if react-dom is enabled streaming rendering mode // Detect if react-dom is enabled streaming rendering mode
const shouldUseReactRoot = !!require('react-dom/server.browser') const shouldUseReactRoot = !!require('react-dom/server').renderToPipeableStream
.renderToReadableStream
// Version is inlined into the file using taskr build pipeline // Version is inlined into the file using taskr build pipeline
if (args['--version']) { if (args['--version']) {
......
...@@ -185,8 +185,8 @@ function createServer(options: NextServerOptions): NextServer { ...@@ -185,8 +185,8 @@ function createServer(options: NextServerOptions): NextServer {
// Make sure env of custom server is overridden. // Make sure env of custom server is overridden.
// Use dynamic require to make sure it's executed in it's own context. // Use dynamic require to make sure it's executed in it's own context.
const ReactDOMServer = require('react-dom/server.browser') const ReactDOMServer = require('react-dom/server')
const shouldUseReactRoot = !!ReactDOMServer.renderToReadableStream const shouldUseReactRoot = !!ReactDOMServer.renderToPipeableStream
if (shouldUseReactRoot) { if (shouldUseReactRoot) {
;(process.env as any).__NEXT_REACT_ROOT = 'true' ;(process.env as any).__NEXT_REACT_ROOT = 'true'
} }
......
...@@ -17,12 +17,14 @@ import { ...@@ -17,12 +17,14 @@ import {
continueFromInitialStream, continueFromInitialStream,
} from './node-web-streams-helper' } from './node-web-streams-helper'
import { FlushEffectsContext } from '../shared/lib/flush-effects' import { FlushEffectsContext } from '../shared/lib/flush-effects'
// @ts-ignore react-dom/client exists when using React 18
import ReactDOMServer from 'react-dom/server.browser'
import { isDynamicRoute } from '../shared/lib/router/utils' import { isDynamicRoute } from '../shared/lib/router/utils'
import { tryGetPreviewData } from './api-utils/node' import { tryGetPreviewData } from './api-utils/node'
import DefaultRootLayout from '../lib/views-layout' import DefaultRootLayout from '../lib/views-layout'
const ReactDOMServer = process.env.__NEXT_REACT_ROOT
? require('react-dom/server.browser')
: require('react-dom/server')
export type RenderOptsPartial = { export type RenderOptsPartial = {
err?: Error | null err?: Error | null
dev?: boolean dev?: boolean
......
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