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

Avoid bundling react-dom/server.browser in Pages router #70463

Closed
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
9 changes: 9 additions & 0 deletions crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,15 @@ async fn insert_next_server_special_aliases(
external_esm_if_node(project_path, "next/dist/compiled/@vercel/og/index.node.js"),
);

import_map.insert_exact_alias(
"next/dist/server/ReactDOMServerPages",
ImportMapping::Alternatives(vec![
request_to_import_mapping(project_path, "react-dom/server.edge"),
request_to_import_mapping(project_path, "react-dom/server.browser"),
])
.cell()
);

import_map.insert_exact_alias(
"@opentelemetry/api",
// It needs to prefer the local version of @opentelemetry/api
Expand Down
9 changes: 9 additions & 0 deletions packages/next/src/build/create-compiler-aliases.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path'
import * as React from 'react'
import {
DOT_NEXT_ALIAS,
PAGES_DIR_ALIAS,
Expand All @@ -21,6 +22,8 @@ interface CompilerAliases {
[alias: string]: string | string[]
}

const isReact19 = typeof React.use === 'function'

export function createWebpackAliases({
distDir,
isClient,
Expand Down Expand Up @@ -90,6 +93,12 @@ export function createWebpackAliases({
return {
'@vercel/og$': 'next/dist/server/og/image-response',

// Avoid bundling both entrypoints in React 19 when we just need one.
// Also avoids bundler warnings in React 18 where react-dom/server.edge doesn't exist.
'next/dist/server/ReactDOMServerPages': isReact19
? 'react-dom/server.edge'
: 'react-dom/server.browser',
Comment on lines +98 to +100
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would use the same approach as Turbopack and use

Suggested change
'next/dist/server/ReactDOMServerPages': isReact19
? 'react-dom/server.edge'
: 'react-dom/server.browser',
'next/dist/server/ReactDOMServerPages': [
'react-dom/server.edge'
'react-dom/server.browser'
],

i.e. fallbacks. But that doesn't seem to work and Webpack still tries to bundle react-dom/server.edge leading to

Module not found: Package path ./server.edge is not exported from package

when running
NEXT_TEST_MODE=dev NEXT_TEST_REACT_VERSION="18.3.1" p test test/e2e/app-dir/app-edge/app-edge.test.ts


// Alias next/dist imports to next/dist/esm assets,
// let this alias hit before `next` alias.
...(isEdgeServer
Expand Down
8 changes: 0 additions & 8 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1974,14 +1974,6 @@ export default async function getBaseWebpackConfig(
)
),
].filter(Boolean as any as ExcludesFalse),
ignoreWarnings: [
(warning) => {
// require('react-dom/server.edge') is wrapped in try-catch so save to ignore.
return warning.message.startsWith(
'Module not found: Error: Package path ./server.edge is not exported from package'
)
},
],
}

// Support tsconfig and jsconfig baseUrl
Expand Down
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 ReactDOMServerPages from './ReactDOMServerPages'
import ReactDOMServerPages from 'next/dist/server/ReactDOMServerPages'
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'
import {
GSP_NO_RETURNED_VALUE,
Expand Down
4 changes: 4 additions & 0 deletions packages/next/types/$$compiled.internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ declare module 'VAR_USERLAND'
declare module 'VAR_MODULE_DOCUMENT'
declare module 'VAR_MODULE_APP'

declare module 'next/dist/server/ReactDOMServerPages' {
export * from 'react-dom/server.edge'
}

declare module 'next/dist/compiled/@napi-rs/triples' {
export * from '@napi-rs/triples'
}
Expand Down
4 changes: 0 additions & 4 deletions packages/next/types/react-dom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ declare module 'react-dom/server.edge' {
>
}

declare module 'react-dom/server.browser' {
export * from 'react-dom/server.edge'
}

declare module 'react-dom/static.edge' {
import type { JSX } from 'react'
/**
Expand Down
Loading