Skip to content

Commit

Permalink
Fallback to react-dom/server.browser in Pages router if Edge versio…
Browse files Browse the repository at this point in the history
…n is not available

`react-dom/server.edge` is not available in React 18.
We were using `.browser` before when we were on React 18 so this should be safe.
  • Loading branch information
eps1lon committed Aug 30, 2024
1 parent ea64a47 commit 2f82cc1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'react-dom/server.edge'
18 changes: 18 additions & 0 deletions packages/next/src/build/shims/ReactDOMServerEdgeIfAvailable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let ReactDOMServer

try {
ReactDOMServer = require('react-dom/server.edge')
} catch (error) {
if (
// TODO: copilot suggestion. Does this code actually exist?
error.code !== 'MODULE_NOT_FOUND' &&
// TODO: actually encountered that
error.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
) {
throw error
}
// TOOD: Ensure App Router does not bundle this
ReactDOMServer = require('react-dom/server.browser')
}

module.exports = ReactDOMServer
2 changes: 1 addition & 1 deletion packages/next/src/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import type { Revalidate, SwrDelta } from './lib/revalidate'
import type { COMPILER_NAMES } from '../shared/lib/constants'

import React, { type JSX } from 'react'
import ReactDOMServerEdge from 'react-dom/server.edge'
import ReactDOMServerEdge from '../build/shims/ReactDOMServerEdgeIfAvailable'
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'
import {
GSP_NO_RETURNED_VALUE,
Expand Down
1 change: 1 addition & 0 deletions packages/next/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const pagesExternals = [
'react-dom/package.json',
'react-dom/client',
'react-dom/server',
'react-dom/server.browser',
'react-dom/server.edge',
'react-server-dom-webpack/client',
'react-server-dom-webpack/client.edge',
Expand Down

0 comments on commit 2f82cc1

Please sign in to comment.