Unverified Commit 5117201d authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[lts-active] Avoid loading standalone React in server bootstrap (#100610)

Backports #100467 and #100401 to `releases/lts/active`.

#100401 defers React loading in `dedupe-fetch.ts`; it builds on #100467,
which separates render validation from dynamic tracking and moves the
fetch-patch marker into a standalone module so server bootstrap imports
cannot reach React. Backported together since the goal of #100401 is
only achieved with the #100467 groundwork.

---------
Co-authored-by: 's avatarJan Kassens <kassens@vercel.com>
Co-authored-by: 's avatarClaude Code <noreply@anthropic.com>
parent 37d603bf
...@@ -180,12 +180,14 @@ import { ...@@ -180,12 +180,14 @@ import {
formatDynamicAPIAccesses, formatDynamicAPIAccesses,
isPrerenderInterruptedError, isPrerenderInterruptedError,
createDynamicTrackingState, createDynamicTrackingState,
consumeDynamicAccess,
type DynamicAccess,
} from './dynamic-rendering'
import {
createDynamicValidationState, createDynamicValidationState,
trackAllowedDynamicAccess, trackAllowedDynamicAccess,
throwIfDisallowedDynamic, throwIfDisallowedDynamic,
PreludeState, PreludeState,
consumeDynamicAccess,
type DynamicAccess,
logDisallowedDynamicError, logDisallowedDynamicError,
trackDynamicHoleInStaticShell, trackDynamicHoleInStaticShell,
getStaticShellDisallowedDynamicReasons, getStaticShellDisallowedDynamicReasons,
...@@ -200,7 +202,7 @@ import { ...@@ -200,7 +202,7 @@ import {
throwIfDisallowedDynamicInStaticRoute, throwIfDisallowedDynamicInStaticRoute,
getDisallowedReasonsInStaticRoute, getDisallowedReasonsInStaticRoute,
type InstantValidationHoleKind, type InstantValidationHoleKind,
} from './dynamic-rendering' } from './dynamic-rendering-validation'
import { logBuildDebugHint } from './blocking-route-messages' import { logBuildDebugHint } from './blocking-route-messages'
import { import {
ClientComponentLoadTracker, ClientComponentLoadTracker,
......
/** /**
* Based on https://github.com/facebook/react/blob/d4e78c42a94be027b4dc7ed2659a5fddfbf9bd4e/packages/react/src/ReactFetch.js * Based on https://github.com/facebook/react/blob/d4e78c42a94be027b4dc7ed2659a5fddfbf9bd4e/packages/react/src/ReactFetch.js
*/ */
import * as React from 'react'
import { cloneResponse } from './clone-response' import { cloneResponse } from './clone-response'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
...@@ -42,6 +41,9 @@ type CacheEntry = [ ...@@ -42,6 +41,9 @@ type CacheEntry = [
] ]
export function createDedupeFetch(originalFetch: typeof fetch) { export function createDedupeFetch(originalFetch: typeof fetch) {
// Importing the helper alone must not load the app-page runtime through
// the App Router's vendored React. Keep the same React instance when used.
const React = require('react') as typeof import('react')
const getCacheEntries = React.cache( const getCacheEntries = React.cache(
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- url is the cache key // eslint-disable-next-line @typescript-eslint/no-unused-vars -- url is the cache key
(url: string): CacheEntry[] => [] (url: string): CacheEntry[] => []
......
export const NEXT_PATCH_SYMBOL = Symbol.for('next-patch')
...@@ -20,6 +20,7 @@ import { markCurrentScopeAsDynamic } from '../app-render/dynamic-rendering' ...@@ -20,6 +20,7 @@ import { markCurrentScopeAsDynamic } from '../app-render/dynamic-rendering'
import { makeDynamicHangingPromise } from '../dynamic-rendering-utils' import { makeDynamicHangingPromise } from '../dynamic-rendering-utils'
import type { FetchMetric } from '../base-http' import type { FetchMetric } from '../base-http'
import { createDedupeFetch } from './dedupe-fetch' import { createDedupeFetch } from './dedupe-fetch'
import { NEXT_PATCH_SYMBOL } from './patch-fetch-symbol'
import { import {
getCacheSignal, getCacheSignal,
type RevalidateStore, type RevalidateStore,
...@@ -87,8 +88,6 @@ type PatchedFetcher = Fetcher & { ...@@ -87,8 +88,6 @@ type PatchedFetcher = Fetcher & {
readonly _nextOriginalFetch: Fetcher readonly _nextOriginalFetch: Fetcher
} }
export const NEXT_PATCH_SYMBOL = Symbol.for('next-patch')
function isFetchPatched() { function isFetchPatched() {
return (globalThis as Record<symbol, unknown>)[NEXT_PATCH_SYMBOL] === true return (globalThis as Record<symbol, unknown>)[NEXT_PATCH_SYMBOL] === true
} }
......
...@@ -53,7 +53,7 @@ import { ...@@ -53,7 +53,7 @@ import {
type AppIsrManifestMessage, type AppIsrManifestMessage,
} from '../dev/hot-reloader-types' } from '../dev/hot-reloader-types'
import { normalizedAssetPrefix } from '../../shared/lib/normalized-asset-prefix' import { normalizedAssetPrefix } from '../../shared/lib/normalized-asset-prefix'
import { NEXT_PATCH_SYMBOL } from './patch-fetch' import { NEXT_PATCH_SYMBOL } from './patch-fetch-symbol'
import type { ServerInitResult } from './render-server' import type { ServerInitResult } from './render-server'
import { filterInternalHeaders } from './server-ipc/utils' import { filterInternalHeaders } from './server-ipc/utils'
import { blockCrossSiteDEV } from './router-utils/block-cross-site-dev' import { blockCrossSiteDEV } from './router-utils/block-cross-site-dev'
......
...@@ -656,7 +656,6 @@ async function readNormalizedNFT(next, name) { ...@@ -656,7 +656,6 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/compiled/ws/index.js", "/node_modules/next/dist/compiled/ws/index.js",
"/node_modules/next/dist/lib/client-and-server-references.js", "/node_modules/next/dist/lib/client-and-server-references.js",
"/node_modules/next/dist/lib/constants.js", "/node_modules/next/dist/lib/constants.js",
"/node_modules/next/dist/lib/framework/boundary-constants.js",
"/node_modules/next/dist/lib/interop-default.js", "/node_modules/next/dist/lib/interop-default.js",
"/node_modules/next/dist/lib/is-error.js", "/node_modules/next/dist/lib/is-error.js",
"/node_modules/next/dist/lib/picocolors.js", "/node_modules/next/dist/lib/picocolors.js",
...@@ -667,15 +666,12 @@ async function readNormalizedNFT(next, name) { ...@@ -667,15 +666,12 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/server/app-render/after-task-async-storage-instance.js", "/node_modules/next/dist/server/app-render/after-task-async-storage-instance.js",
"/node_modules/next/dist/server/app-render/after-task-async-storage.external.js", "/node_modules/next/dist/server/app-render/after-task-async-storage.external.js",
"/node_modules/next/dist/server/app-render/async-local-storage.js", "/node_modules/next/dist/server/app-render/async-local-storage.js",
"/node_modules/next/dist/server/app-render/blocking-route-messages.js",
"/node_modules/next/dist/server/app-render/cache-signal.js", "/node_modules/next/dist/server/app-render/cache-signal.js",
"/node_modules/next/dist/server/app-render/console-async-storage-instance.js", "/node_modules/next/dist/server/app-render/console-async-storage-instance.js",
"/node_modules/next/dist/server/app-render/console-async-storage.external.js", "/node_modules/next/dist/server/app-render/console-async-storage.external.js",
"/node_modules/next/dist/server/app-render/dynamic-access-async-storage-instance.js", "/node_modules/next/dist/server/app-render/dynamic-access-async-storage-instance.js",
"/node_modules/next/dist/server/app-render/dynamic-access-async-storage.external.js", "/node_modules/next/dist/server/app-render/dynamic-access-async-storage.external.js",
"/node_modules/next/dist/server/app-render/dynamic-rendering.js", "/node_modules/next/dist/server/app-render/dynamic-rendering.js",
"/node_modules/next/dist/server/app-render/instant-validation/boundary-constants.js",
"/node_modules/next/dist/server/app-render/instant-validation/boundary-tracking.js",
"/node_modules/next/dist/server/app-render/module-loading/track-module-loading.external.js", "/node_modules/next/dist/server/app-render/module-loading/track-module-loading.external.js",
"/node_modules/next/dist/server/app-render/module-loading/track-module-loading.instance.js", "/node_modules/next/dist/server/app-render/module-loading/track-module-loading.instance.js",
"/node_modules/next/dist/server/app-render/segment-config/ensure-static.js", "/node_modules/next/dist/server/app-render/segment-config/ensure-static.js",
...@@ -733,11 +729,9 @@ async function readNormalizedNFT(next, name) { ...@@ -733,11 +729,9 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/server/runtime-reacts.external.js", "/node_modules/next/dist/server/runtime-reacts.external.js",
"/node_modules/next/dist/shared/lib/deep-freeze.js", "/node_modules/next/dist/shared/lib/deep-freeze.js",
"/node_modules/next/dist/shared/lib/i18n/normalize-locale-path.js", "/node_modules/next/dist/shared/lib/i18n/normalize-locale-path.js",
"/node_modules/next/dist/shared/lib/instant-messages.js",
"/node_modules/next/dist/shared/lib/invariant-error.js", "/node_modules/next/dist/shared/lib/invariant-error.js",
"/node_modules/next/dist/shared/lib/is-plain-object.js", "/node_modules/next/dist/shared/lib/is-plain-object.js",
"/node_modules/next/dist/shared/lib/is-thenable.js", "/node_modules/next/dist/shared/lib/is-thenable.js",
"/node_modules/next/dist/shared/lib/lazy-dynamic/react-browser-bailout.js",
"/node_modules/next/dist/shared/lib/no-fallback-error.external.js", "/node_modules/next/dist/shared/lib/no-fallback-error.external.js",
"/node_modules/next/dist/shared/lib/page-path/ensure-leading-slash.js", "/node_modules/next/dist/shared/lib/page-path/ensure-leading-slash.js",
"/node_modules/next/dist/shared/lib/page-path/normalize-page-path.js", "/node_modules/next/dist/shared/lib/page-path/normalize-page-path.js",
...@@ -751,9 +745,6 @@ async function readNormalizedNFT(next, name) { ...@@ -751,9 +745,6 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/shared/lib/segment.js", "/node_modules/next/dist/shared/lib/segment.js",
"/node_modules/next/dist/shared/lib/server-reference-info.js", "/node_modules/next/dist/shared/lib/server-reference-info.js",
"/node_modules/next/dist/shared/lib/utils.js", "/node_modules/next/dist/shared/lib/utils.js",
"/node_modules/react/cjs/react.development.js",
"/node_modules/react/cjs/react.production.js",
"/node_modules/react/index.js",
] ]
`) `)
}) })
......
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