Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: react root enabled properly in custom server #36664

Merged
merged 4 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/next/client/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* global location */
import '../build/polyfills/polyfill-module'
import React, { useState } from 'react'
import ReactDOM from 'react-dom'
import { HeadManagerContext } from '../shared/lib/head-manager-context'
import mitt, { MittEmitter } from '../shared/lib/mitt'
import { RouterContext } from '../shared/lib/router-context'
Expand Down Expand Up @@ -41,6 +40,10 @@ import { RefreshContext } from './streaming/refresh'
import { ImageConfigContext } from '../shared/lib/image-config-context'
import { ImageConfigComplete } from '../shared/lib/image-config'

const ReactDOM = process.env.__NEXT_REACT_ROOT
? require('react-dom/client')
: require('react-dom')

/// <reference types="react-dom/experimental" />

declare let __webpack_public_path__: string
Expand Down Expand Up @@ -549,8 +552,7 @@ function renderReactElement(
if (process.env.__NEXT_REACT_ROOT) {
if (!reactRoot) {
// Unlike with createRoot, you don't need a separate root.render() call here
const ReactDOMClient = require('react-dom/client')
reactRoot = ReactDOMClient.hydrateRoot(domEl, reactEl)
reactRoot = ReactDOM.hydrateRoot(domEl, reactEl)
// TODO: Remove shouldHydrate variable when React 18 is stable as it can depend on `reactRoot` existing
shouldHydrate = false
} else {
Expand Down
8 changes: 8 additions & 0 deletions packages/next/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ function createServer(options: NextServerOptions): NextServer {
)
}

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

return new NextServer(options)
}

Expand Down
10 changes: 10 additions & 0 deletions test/integration/custom-server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,18 @@ describe('Custom Server', () => {
try {
browser = await webdriver(context.appPort, '/test-index-hmr')
const text = await browser.elementByCss('#go-asset').text()
const logs = await browser.log()
expect(text).toBe('Asset')

// Hydrates with react 18 is correct as expected
expect(
logs.some((log) =>
log.message.includes(
'ReactDOM.hydrate is no longer supported in React 18'
)
)
).toBe(false)

indexPg.replace('Asset', 'Asset!!')

await check(() => browser.elementByCss('#go-asset').text(), /Asset!!/)
Expand Down