Unverified Commit 85f92319 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Avoid bundling client-nodemodules used by `unstable_rethrow` in client-browser modules (#95196)

There are more client-node modules being bundled into `client-browser`
which I'll fix in a followup (with codegen).

Our internal `unstable-rethrow` will now have a version without suffix
(unbundled or client-node) and a client-browser only version
(`.browser`) that bundlers alias to for client-browser which will always
be bundled.

For the specific modules being removed from client-browser, check the
second commit with the actual change. The first commit fixes the
`browser-chunk` test to include `next/src/server` in a snapshot.

Closes #95189
parent f23a96f0
...@@ -162,6 +162,7 @@ pub async fn get_client_resolve_options_context( ...@@ -162,6 +162,7 @@ pub async fn get_client_resolve_options_context(
.await?; .await?;
let next_client_resolved_map = let next_client_resolved_map =
get_next_client_resolved_map(project_path.clone(), project_path.clone(), *mode.await?) get_next_client_resolved_map(project_path.clone(), project_path.clone(), *mode.await?)
.await?
.to_resolved() .to_resolved()
.await?; .await?;
let mut custom_conditions: Vec<_> = mode.await?.custom_resolve_conditions().collect(); let mut custom_conditions: Vec<_> = mode.await?.custom_resolve_conditions().collect();
......
...@@ -7,7 +7,11 @@ use next_taskless::{EDGE_NODE_EXTERNALS, NODE_EXTERNALS}; ...@@ -7,7 +7,11 @@ use next_taskless::{EDGE_NODE_EXTERNALS, NODE_EXTERNALS};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{FxIndexMap, ResolvedVc, Vc, fxindexmap}; use turbo_tasks::{FxIndexMap, ResolvedVc, Vc, fxindexmap};
use turbo_tasks_fs::{FileContent, FileSystem, FileSystemPath, to_sys_path}; use turbo_tasks_fs::{
FileContent, FileSystem, FileSystemPath,
glob::{Glob, GlobOptions},
to_sys_path,
};
use turbopack_core::{ use turbopack_core::{
asset::AssetContent, asset::AssetContent,
issue::{Issue, IssueExt, IssueSeverity, IssueStage, StyledString}, issue::{Issue, IssueExt, IssueSeverity, IssueStage, StyledString},
...@@ -559,16 +563,36 @@ async fn insert_unsupported_node_internal_aliases(import_map: &mut ImportMap) -> ...@@ -559,16 +563,36 @@ async fn insert_unsupported_node_internal_aliases(import_map: &mut ImportMap) ->
Ok(()) Ok(())
} }
pub fn get_next_client_resolved_map( pub async fn get_next_client_resolved_map(
_context: FileSystemPath, context_path: FileSystemPath,
_root: FileSystemPath, root: FileSystemPath,
_mode: NextMode, _mode: NextMode,
) -> Vc<ResolvedMap> { ) -> Result<Vc<ResolvedMap>> {
let glob_mappings = vec![]; // In the browser bundle, swap the default `unstable-rethrow` (which holds the full
ResolvedMap { // server logic) for its `.browser` sibling. The server-only checks can never occur in
// the browser, and bundling the default would drag server-only modules into the client
// bundle. This is the Turbopack analog of the webpack alias in `create-compiler-aliases.ts`
// and is client-only because `get_next_client_resolved_map` is used only by the client
// context. Matching is on the resolved file path, so it intercepts the relative import
// regardless of which module pulls it in. Anchored at the filesystem root so it matches
// wherever `next` resolves from (node_modules, pnpm store, or monorepo `packages/next`).
let glob_mappings = vec![(
root.root().owned().await?,
Glob::new(
rcstr!("**/next/dist/client/components/unstable-rethrow.js"),
GlobOptions::default(),
)
.to_resolved()
.await?,
request_to_import_mapping(
context_path,
rcstr!("next/dist/client/components/unstable-rethrow.browser"),
),
)];
Ok(ResolvedMap {
by_glob: glob_mappings, by_glob: glob_mappings,
} }
.cell() .cell())
} }
static NEXT_ALIASES: LazyLock<[(RcStr, RcStr); 23]> = LazyLock::new(|| { static NEXT_ALIASES: LazyLock<[(RcStr, RcStr); 23]> = LazyLock::new(|| {
......
...@@ -16,7 +16,10 @@ import { ...@@ -16,7 +16,10 @@ import {
import type { NextConfigComplete } from '../server/config-shared' import type { NextConfigComplete } from '../server/config-shared'
import { defaultOverrides } from '../server/require-hook' import { defaultOverrides } from '../server/require-hook'
import { hasExternalOtelApiPackage } from './webpack-config' import { hasExternalOtelApiPackage } from './webpack-config'
import { NEXT_PROJECT_ROOT } from './next-dir-paths' import {
NEXT_PROJECT_ROOT,
NEXT_PROJECT_ROOT_DIST_CLIENT,
} from './next-dir-paths'
import { shouldUseReactServerCondition } from './utils' import { shouldUseReactServerCondition } from './utils'
interface CompilerAliases { interface CompilerAliases {
...@@ -166,6 +169,16 @@ export function createWebpackAliases({ ...@@ -166,6 +169,16 @@ export function createWebpackAliases({
// disable typechecker, webpack5 allows aliases to be set to false to create a no-op module // disable typechecker, webpack5 allows aliases to be set to false to create a no-op module
'private-next-empty-module': false as any, 'private-next-empty-module': false as any,
// In the browser bundle, swap the default `unstable-rethrow` (which holds the
// full server logic) for its `.browser` sibling. The server checks can never
// occur in the browser, and bundling the default would drag server-only modules
// into the client bundle. Server/edge compilers are not aliased and keep the
// default. The trailing `$` is an exact match so it cannot catch `.browser.js`.
[path.join(
NEXT_PROJECT_ROOT_DIST_CLIENT,
'components/unstable-rethrow.js'
) + '$']: 'next/dist/client/components/unstable-rethrow.browser',
} }
: {}), : {}),
......
import { isHangingPromiseRejectionError } from '../../server/dynamic-rendering-utils'
import { isPostpone } from '../../server/lib/router-utils/is-postpone'
import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
import { isNextRouterError } from './is-next-router-error'
import {
isDynamicPostpone,
isPrerenderInterruptedError,
} from '../../server/app-render/dynamic-rendering'
import { isDynamicServerError } from './hooks-server-context'
export function unstable_rethrow(error: unknown): void {
if (
isNextRouterError(error) ||
isBailoutToCSRError(error) ||
isDynamicServerError(error) ||
isDynamicPostpone(error) ||
isPostpone(error) ||
isHangingPromiseRejectionError(error) ||
isPrerenderInterruptedError(error)
) {
throw error
}
if (error instanceof Error && 'cause' in error) {
unstable_rethrow(error.cause)
}
}
import { isHangingPromiseRejectionError } from '../../server/dynamic-rendering-utils'
import { isPostpone } from '../../server/lib/router-utils/is-postpone'
import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
import { isNextRouterError } from './is-next-router-error'
import {
isDynamicPostpone,
isPrerenderInterruptedError,
} from '../../server/app-render/dynamic-rendering'
import { isDynamicServerError } from './hooks-server-context'
/** /**
* This function should be used to rethrow internal Next.js errors so that they can be handled by the framework. * This function should be used to rethrow internal Next.js errors so that they can be handled by the framework.
* When wrapping an API that uses errors to interrupt control flow, you should use this function before you do any error handling. * When wrapping an API that uses errors to interrupt control flow, you should use this function before you do any error handling.
* This function will rethrow the error if it is a Next.js error so it can be handled, otherwise it will do nothing. * This function will rethrow the error if it is a Next.js error so it can be handled, otherwise it will do nothing.
* *
* In the browser bundle this module is aliased to `./unstable-rethrow.browser`, which performs a
* subset of these checks (the server-only ones can never occur in the browser). This default
* module holds the full server logic and is used on every server runtime (Node, edge) and in any
* context where the alias does not apply.
*
* Read more: [Next.js Docs: `unstable_rethrow`](https://nextjs.org/docs/app/api-reference/functions/unstable_rethrow) * Read more: [Next.js Docs: `unstable_rethrow`](https://nextjs.org/docs/app/api-reference/functions/unstable_rethrow)
*/ */
export const unstable_rethrow = export function unstable_rethrow(error: unknown): void {
typeof window === 'undefined' if (
? ( isNextRouterError(error) ||
require('./unstable-rethrow.server') as typeof import('./unstable-rethrow.server') isBailoutToCSRError(error) ||
).unstable_rethrow isDynamicServerError(error) ||
: ( isDynamicPostpone(error) ||
require('./unstable-rethrow.browser') as typeof import('./unstable-rethrow.browser') isPostpone(error) ||
).unstable_rethrow isHangingPromiseRejectionError(error) ||
isPrerenderInterruptedError(error)
) {
throw error
}
if (error instanceof Error && 'cause' in error) {
unstable_rethrow(error.cause)
}
}
...@@ -3,6 +3,22 @@ import { join } from 'path' ...@@ -3,6 +3,22 @@ import { join } from 'path'
import { readFile } from 'fs/promises' import { readFile } from 'fs/promises'
import { listClientChunks } from 'next-test-utils' import { listClientChunks } from 'next-test-utils'
// Normalize a sourcemap `sources` entry to a path relative to the Next.js
// package root (e.g. `src/...`, `dist/...`), so the path filters below behave
// consistently across bundlers and produce stable snapshots. Webpack emits
// `webpack://_N_E/../../src/...`; Turbopack emits
// `turbopack:///[project]/<...>/packages/next/src/...`.
function normalizeSource(source: string): string {
return source
.replace(/\?.*$/, '')
.replace(/^webpack:\/\/_N_E\//, '')
.replace(/^turbopack:\/\/\//, '')
.replace(/^\[project\]\//, '')
.replace(/^(?:\.\.\/)+/, '')
.replace(/^.*?packages\/next\//, '')
.replace(/^.*?\/node_modules\/next\//, '')
}
describe('browser-chunks', () => { describe('browser-chunks', () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
...@@ -29,68 +45,150 @@ describe('browser-chunks', () => { ...@@ -29,68 +45,150 @@ describe('browser-chunks', () => {
) )
}) })
// These snapshots document which matching modules currently reach browser
// chunks. Some of these we don't intend to act on yet, so we snapshot the
// normalized paths (rather than hard-fail) to surface regressions on review.
it('must not bundle any server modules into browser chunks', () => { it('must not bundle any server modules into browser chunks', () => {
const serverSources = sources.filter( const serverSources = Array.from(
(source) => new Set(
/webpack:\/\/_N_E\/(\.\.\/)*src\/server\//.test(source) || sources
source.includes('next/dist/esm/server') || .map(normalizeSource)
source.includes('next/dist/server') || .filter(
source.includes('next-devtools/server') (source) =>
) source.startsWith('src/server/') ||
source.startsWith('dist/esm/server/') ||
if (serverSources.length > 0) { source.startsWith('dist/server/') ||
console.error( source.includes('next-devtools/server')
`Found the following server modules:\n ${serverSources.join('\n ')}\nIf any of these modules are allowed to be included in browser chunks, move them to src/shared or src/client.` )
) )
).sort()
throw new Error('Did not expect any server modules in browser chunks.')
// This set varies along two axes, so snapshot each combination separately
// rather than forcing them to agree:
// - bundler: webpack's browser chunks contain none of these; Turbopack
// still pulls in a set we haven't acted on yet.
// - cache components: enabling it pulls additional server modules
// (instant-validation, async storage, dynamic rendering) into the
// client render path. CI runs this suite both with and without it
// (see test/cache-components-tests-manifest.json).
const cacheComponents = process.env.__NEXT_CACHE_COMPONENTS === 'true'
if (process.env.IS_TURBOPACK_TEST) {
if (cacheComponents) {
expect(serverSources).toMatchInlineSnapshot(`
[
"src/server/app-render/action-async-storage-instance.ts",
"src/server/app-render/action-async-storage.external.ts",
"src/server/app-render/after-task-async-storage-instance.ts",
"src/server/app-render/after-task-async-storage.external.ts",
"src/server/app-render/async-local-storage.ts",
"src/server/app-render/blocking-route-messages.ts",
"src/server/app-render/dynamic-access-async-storage-instance.ts",
"src/server/app-render/dynamic-access-async-storage.external.ts",
"src/server/app-render/dynamic-rendering.ts",
"src/server/app-render/instant-validation/boundary-constants.ts",
"src/server/app-render/instant-validation/boundary-impl.tsx",
"src/server/app-render/instant-validation/boundary-tracking.tsx",
"src/server/app-render/instant-validation/instant-samples-client.ts",
"src/server/app-render/instant-validation/instant-samples.ts",
"src/server/app-render/instant-validation/instant-validation-error.ts",
"src/server/app-render/staged-rendering.ts",
"src/server/app-render/vary-params.ts",
"src/server/app-render/work-async-storage-instance.ts",
"src/server/app-render/work-async-storage.external.ts",
"src/server/app-render/work-unit-async-storage-instance.ts",
"src/server/app-render/work-unit-async-storage.external.ts",
"src/server/create-deduped-by-callsite-server-error-logger.ts",
"src/server/dynamic-rendering-utils.ts",
"src/server/request/params.ts",
"src/server/request/search-params.ts",
"src/server/request/utils.ts",
"src/server/runtime-reacts.external.ts",
"src/server/web/spec-extension/adapters/headers.ts",
"src/server/web/spec-extension/adapters/reflect.ts",
"src/server/web/spec-extension/adapters/request-cookies.ts",
"src/server/web/spec-extension/cookies.ts",
]
`)
} else {
expect(serverSources).toMatchInlineSnapshot(`
[
"src/server/app-render/action-async-storage-instance.ts",
"src/server/app-render/action-async-storage.external.ts",
"src/server/app-render/after-task-async-storage-instance.ts",
"src/server/app-render/after-task-async-storage.external.ts",
"src/server/app-render/async-local-storage.ts",
"src/server/app-render/blocking-route-messages.ts",
"src/server/app-render/dynamic-access-async-storage-instance.ts",
"src/server/app-render/dynamic-access-async-storage.external.ts",
"src/server/app-render/dynamic-rendering.ts",
"src/server/app-render/instant-validation/boundary-constants.ts",
"src/server/app-render/instant-validation/boundary-tracking.tsx",
"src/server/app-render/instant-validation/instant-samples.ts",
"src/server/app-render/instant-validation/instant-validation-error.ts",
"src/server/app-render/staged-rendering.ts",
"src/server/app-render/vary-params.ts",
"src/server/app-render/work-async-storage-instance.ts",
"src/server/app-render/work-async-storage.external.ts",
"src/server/app-render/work-unit-async-storage-instance.ts",
"src/server/app-render/work-unit-async-storage.external.ts",
"src/server/create-deduped-by-callsite-server-error-logger.ts",
"src/server/dynamic-rendering-utils.ts",
"src/server/request/params.ts",
"src/server/request/search-params.ts",
"src/server/request/utils.ts",
"src/server/runtime-reacts.external.ts",
"src/server/web/spec-extension/adapters/headers.ts",
"src/server/web/spec-extension/adapters/reflect.ts",
"src/server/web/spec-extension/adapters/request-cookies.ts",
"src/server/web/spec-extension/cookies.ts",
]
`)
}
} else {
if (cacheComponents) {
expect(serverSources).toMatchInlineSnapshot(`[]`)
} else {
expect(serverSources).toMatchInlineSnapshot(`[]`)
}
} }
}) })
it('must not bundle any dev overlay into browser chunks', () => { it('must not bundle any dev overlay into browser chunks', () => {
const devOverlaySources = sources.filter((source) => { const devOverlaySources = Array.from(
return source.includes('next-devtools') new Set(
}) sources
.map(normalizeSource)
if (devOverlaySources.length > 0) { .filter((source) => source.includes('next-devtools'))
const message = `Found the following dev overlay modules:\n ${devOverlaySources.join('\n')}`
console.error(
`${message}\nIf any of these modules are allowed to be included in production chunks, check the import and render conditions.`
) )
).sort()
throw new Error( expect(devOverlaySources).toMatchInlineSnapshot(`[]`)
'Did not expect any dev overlay modules in browser chunks.\n' + message
)
}
}) })
it('must not bundle the HMR refresh reducer into browser chunks', () => { it('must not bundle the HMR refresh reducer into browser chunks', () => {
const hmrReducerSources = sources.filter((source) => { const hmrReducerSources = Array.from(
return source.includes('hmr-refresh-reducer') new Set(
}) sources
.map(normalizeSource)
if (hmrReducerSources.length > 0) { .filter((source) => source.includes('hmr-refresh-reducer'))
const message = `Found the following HMR reducer modules:\n ${hmrReducerSources.join('\n ')}`
throw new Error(
'Did not expect the HMR refresh reducer in production browser chunks.\n' +
message
) )
} ).sort()
expect(hmrReducerSources).toMatchInlineSnapshot(`[]`)
}) })
it('must not include heavy dependencies into browser chunks', () => { it('must not include heavy dependencies into browser chunks', () => {
const heavyDependencies = sources.filter((source) => { const heavyDependencies = Array.from(
return source.includes('next/dist/compiled/safe-stable-stringify') new Set(
}) sources
.map(normalizeSource)
if (heavyDependencies.length > 0) { .filter((source) =>
const message = `Found the following heavy dependencies:\n ${heavyDependencies.join('\n ')}` source.includes('dist/compiled/safe-stable-stringify')
)
throw new Error(
'Did not expect any heavy dependencies in browser chunks.\n' + message
) )
} ).sort()
expect(heavyDependencies).toMatchInlineSnapshot(`[]`)
}) })
it('must not pull server internals from next/cache into browser chunks', () => { it('must not pull server internals from next/cache into browser chunks', () => {
......
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