#!/bin/bash # Cross-platform sed -i wrapper (macOS needs '' arg, Linux doesn't) sedi() { if [[ "$OSTYPE" == darwin* ]]; then sed -i '' "$@"; else sed -i "$@"; fi; } # Patch React RSC dev runtime to prevent O(n^2) memory growth. # # Root cause: In dev mode, React serializes full component props into debug # chunks via componentDebugInfo.props=props. As the component tree grows, # each component's debug info includes references to ancestor data, causing # super-linear memory growth. With ~100 components, the server OOMs at 2GB+. # # Fix: Replace props=props with props={} in the debug info assignment. # This preserves all other debug functionality (component names, stack traces, # owner chains) while preventing the quadratic memory explosion. # # Affects: next/dist/compiled/next-server/app-page-turbo.runtime.dev.js # (the actual RSC server runtime loaded by Turbopack in dev mode) TARGET="node_modules/next/dist/compiled/next-server/app-page-turbo.runtime.dev.js" if [ ! -f "$TARGET" ]; then echo "[patch-next] Target file not found: $TARGET" exit 0 fi SEARCH="componentDebugInfo.props=props,componentDebugInfo.debugStack" REPLACE="componentDebugInfo.props={},componentDebugInfo.debugStack" if grep -q "$REPLACE" "$TARGET"; then echo "[patch-next] OOM patch already applied." elif ! grep -q "$SEARCH" "$TARGET"; then echo "[patch-next] WARNING: Search string not found — Next.js version may have changed." else sedi "s|$SEARCH|$REPLACE|" "$TARGET" echo "[patch-next] Patched componentDebugInfo.props to prevent RSC dev-mode OOM." fi # --------------------------------------------------------------------------- # Patch 2: Fix incomplete hook chain on unwind (React #33580) # # When a component suspends mid-render, the work-in-progress fiber has an # incomplete hook chain. If this fiber is later committed (e.g. Suspense # fallback), the incomplete chain replaces the current fiber's complete chain, # causing "Rendered more hooks than during the previous render" on re-render. # # Fix: In resetHooksOnUnwind, clone remaining hooks from the current fiber # onto the work-in-progress chain before clearing state. # --------------------------------------------------------------------------- HOOKS_SEARCH='function resetHooksOnUnwind(workInProgress) {' HOOKS_INSERT='if (currentHook !== null \&\& workInProgressHook !== null) { var nextCurrentHook = currentHook.next; if (nextCurrentHook !== null) { var tail = workInProgressHook; while (nextCurrentHook !== null) { var clone = { memoizedState: nextCurrentHook.memoizedState, baseState: nextCurrentHook.baseState, baseQueue: nextCurrentHook.baseQueue, queue: nextCurrentHook.queue, next: null }; tail = tail.next = clone; nextCurrentHook = nextCurrentHook.next; } } }' HOOKS_PATCHED=0 for f in node_modules/next/dist/compiled/react-dom/cjs/react-dom-client.production.js \ node_modules/next/dist/compiled/react-dom/cjs/react-dom-client.development.js \ node_modules/next/dist/compiled/react-dom/cjs/react-dom-profiling.profiling.js \ node_modules/next/dist/compiled/react-dom/cjs/react-dom-profiling.development.js \ node_modules/next/dist/compiled/react-dom-experimental/cjs/react-dom-client.production.js \ node_modules/next/dist/compiled/react-dom-experimental/cjs/react-dom-client.development.js \ node_modules/next/dist/compiled/react-dom-experimental/cjs/react-dom-profiling.profiling.js \ node_modules/next/dist/compiled/react-dom-experimental/cjs/react-dom-profiling.development.js \ node_modules/next/dist/compiled/react-dom-experimental/cjs/react-dom-unstable_testing.production.js \ node_modules/next/dist/compiled/react-dom-experimental/cjs/react-dom-unstable_testing.development.js; do [ ! -f "$f" ] && continue # Skip if already patched grep -q 'clone remaining hooks from current fiber' "$f" 2>/dev/null && continue if ! grep -q "$HOOKS_SEARCH" "$f"; then echo "[patch-next] WARNING: resetHooksOnUnwind not found in $f" continue fi # Insert the fix after the function opening brace sedi "s|$HOOKS_SEARCH|$HOOKS_SEARCH /* clone remaining hooks from current fiber (#33580) */ $HOOKS_INSERT|" "$f" HOOKS_PATCHED=$((HOOKS_PATCHED + 1)) done if [ "$HOOKS_PATCHED" -gt 0 ]; then echo "[patch-next] Patched resetHooksOnUnwind in $HOOKS_PATCHED files (React #33580)." else echo "[patch-next] resetHooksOnUnwind already patched or not found." fi # --------------------------------------------------------------------------- # Patch 3: Suppress INLAY_MISSING errors from dev error overlay # # We use errors with digest starting with "INLAY_MISSING" as control flow # (similar to how Next.js uses NEXT_REDIRECT / NEXT_NOT_FOUND). These get # caught by our error boundaries and rendered as placeholder UI, but the # dev overlay still picks them up and shows a red error screen. # # Fix: In the React root error callbacks (onCaughtError / onUncaughtError), # add an INLAY_MISSING digest check alongside the existing isNextRouterError # check so these errors are suppressed from the overlay. # --------------------------------------------------------------------------- OVERLAY_TARGET="node_modules/next/dist/client/react-client-callbacks/error-boundary-callbacks.js" OVERLAY_SEARCH='isNextRouterError)(thrownValue)) return' OVERLAY_REPLACE='isNextRouterError)(thrownValue) || thrownValue?.digest?.startsWith("INLAY_MISSING")) return' if [ ! -f "$OVERLAY_TARGET" ]; then echo "[patch-next] error-boundary-callbacks.js not found, skipping overlay patch." elif grep -q 'INLAY_MISSING' "$OVERLAY_TARGET"; then echo "[patch-next] INLAY_MISSING overlay patch already applied." elif ! grep -qF "$OVERLAY_SEARCH" "$OVERLAY_TARGET"; then echo "[patch-next] WARNING: overlay patch search string not found — Next.js version may have changed." else sedi "s#$OVERLAY_SEARCH#$OVERLAY_REPLACE#g" "$OVERLAY_TARGET" echo "[patch-next] Patched error-boundary-callbacks.js to suppress INLAY_MISSING from dev overlay." fi