Skip to content

Commit

Permalink
Merge branch 'canary' into fix/app-doc-check
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed May 3, 2022
2 parents a9b0cda + ce6e8b5 commit 3470850
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/next/client/index.tsx
@@ -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
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
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

0 comments on commit 3470850

Please sign in to comment.