Skip to content

Commit 98dee9f

Browse files
committed
Track the inner most debugTask parent
1 parent cfb8945 commit 98dee9f

File tree

1 file changed

+84
-48
lines changed

1 file changed

+84
-48
lines changed

packages/react-server/src/ReactFizzServer.js

Lines changed: 84 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ type RenderTask = {
250250
thenableState: null | ThenableState,
251251
isFallback: boolean, // whether this task is rendering inside a fallback tree
252252
legacyContext: LegacyContext, // the current legacy context that this task is executing in
253+
debugTask: null | ConsoleTask, // DEV only
253254
// DON'T ANY MORE FIELDS. We at 16 already which otherwise requires converting to a constructor.
254255
// Consider splitting into multiple objects or consolidating some fields.
255256
};
@@ -279,6 +280,7 @@ type ReplayTask = {
279280
thenableState: null | ThenableState,
280281
isFallback: boolean, // whether this task is rendering inside a fallback tree
281282
legacyContext: LegacyContext, // the current legacy context that this task is executing in
283+
debugTask: null | ConsoleTask, // DEV only
282284
// DON'T ANY MORE FIELDS. We at 16 already which otherwise requires converting to a constructor.
283285
// Consider splitting into multiple objects or consolidating some fields.
284286
};
@@ -468,6 +470,7 @@ function RequestInstance(
468470
null,
469471
false,
470472
emptyContextObject,
473+
null,
471474
);
472475
pingedTasks.push(rootTask);
473476
}
@@ -610,6 +613,7 @@ export function resumeRequest(
610613
null,
611614
false,
612615
emptyContextObject,
616+
null,
613617
);
614618
pingedTasks.push(rootTask);
615619
return request;
@@ -636,6 +640,7 @@ export function resumeRequest(
636640
null,
637641
false,
638642
emptyContextObject,
643+
null,
639644
);
640645
pingedTasks.push(rootTask);
641646
return request;
@@ -704,6 +709,7 @@ function createRenderTask(
704709
componentStack: null | ComponentStackNode,
705710
isFallback: boolean,
706711
legacyContext: LegacyContext,
712+
debugTask: null | ConsoleTask,
707713
): RenderTask {
708714
request.allPendingTasks++;
709715
if (blockedBoundary === null) {
@@ -731,6 +737,9 @@ function createRenderTask(
731737
if (!disableLegacyContext) {
732738
task.legacyContext = legacyContext;
733739
}
740+
if (__DEV__ && enableOwnerStacks) {
741+
task.debugTask = debugTask;
742+
}
734743
abortSet.add(task);
735744
return task;
736745
}
@@ -751,6 +760,7 @@ function createReplayTask(
751760
componentStack: null | ComponentStackNode,
752761
isFallback: boolean,
753762
legacyContext: LegacyContext,
763+
debugTask: null | ConsoleTask,
754764
): ReplayTask {
755765
request.allPendingTasks++;
756766
if (blockedBoundary === null) {
@@ -779,6 +789,9 @@ function createReplayTask(
779789
if (!disableLegacyContext) {
780790
task.legacyContext = legacyContext;
781791
}
792+
if (__DEV__ && enableOwnerStacks) {
793+
task.debugTask = debugTask;
794+
}
782795
abortSet.add(task);
783796
return task;
784797
}
@@ -887,40 +900,44 @@ function createClassComponentStack(
887900
type,
888901
};
889902
}
890-
function createServerComponentStack(
903+
function pushServerComponentStack(
891904
task: Task,
892905
debugInfo: void | null | ReactDebugInfo,
893-
): null | ComponentStackNode {
906+
): void {
907+
if (!__DEV__) {
908+
// eslint-disable-next-line react-internal/prod-error-codes
909+
throw new Error(
910+
'createServerComponentStack should never be called in production. This is a bug in React.',
911+
);
912+
}
894913
// Build a Server Component parent stack from the debugInfo.
895-
if (__DEV__) {
896-
let node = task.componentStack;
897-
if (debugInfo != null) {
898-
const stack: ReactDebugInfo = debugInfo;
899-
for (let i = 0; i < stack.length; i++) {
900-
const componentInfo: ReactComponentInfo = (stack[i]: any);
901-
if (typeof componentInfo.name !== 'string') {
902-
continue;
903-
}
904-
let name = componentInfo.name;
905-
const env = componentInfo.env;
906-
if (env) {
907-
name += ' (' + env + ')';
908-
}
909-
node = {
910-
tag: 3,
911-
parent: node,
912-
type: name,
913-
owner: componentInfo.owner,
914-
stack: componentInfo.stack,
915-
};
914+
if (debugInfo != null) {
915+
const stack: ReactDebugInfo = debugInfo;
916+
for (let i = 0; i < stack.length; i++) {
917+
const componentInfo: ReactComponentInfo = (stack[i]: any);
918+
if (typeof componentInfo.name !== 'string') {
919+
continue;
920+
}
921+
if (enableOwnerStacks && componentInfo.stack === undefined) {
922+
continue;
923+
}
924+
let name = componentInfo.name;
925+
const env = componentInfo.env;
926+
if (env) {
927+
name += ' (' + env + ')';
928+
}
929+
task.componentStack = {
930+
tag: 3,
931+
parent: task.componentStack,
932+
type: name,
933+
owner: componentInfo.owner,
934+
stack: componentInfo.stack,
935+
};
936+
if (enableOwnerStacks) {
937+
task.debugTask = (componentInfo.task: any);
916938
}
917939
}
918-
return node;
919940
}
920-
// eslint-disable-next-line react-internal/prod-error-codes
921-
throw new Error(
922-
'createServerComponentStack should never be called in production. This is a bug in React.',
923-
);
924941
}
925942

926943
function createComponentStackFromType(
@@ -1231,6 +1248,7 @@ function renderSuspenseBoundary(
12311248
suspenseComponentStack,
12321249
true,
12331250
!disableLegacyContext ? task.legacyContext : emptyContextObject,
1251+
__DEV__ && enableOwnerStacks ? task.debugTask : null,
12341252
);
12351253
// TODO: This should be queued at a separate lower priority queue so that we only work
12361254
// on preparing fallbacks if we don't have any more main content to task on.
@@ -1371,6 +1389,7 @@ function replaySuspenseBoundary(
13711389
suspenseComponentStack,
13721390
true,
13731391
!disableLegacyContext ? task.legacyContext : emptyContextObject,
1392+
__DEV__ && enableOwnerStacks ? task.debugTask : null,
13741393
);
13751394
// TODO: This should be queued at a separate lower priority queue so that we only work
13761395
// on preparing fallbacks if we don't have any more main content to task on.
@@ -2570,21 +2589,24 @@ function renderNodeDestructive(
25702589
const owner = __DEV__ ? element._owner : null;
25712590
const stack = __DEV__ && enableOwnerStacks ? element._debugStack : null;
25722591

2592+
let previousDebugTask: null | ConsoleTask = null;
25732593
const previousComponentStack = task.componentStack;
2594+
let debugTask: null | ConsoleTask;
25742595
if (__DEV__) {
2575-
task.componentStack = createServerComponentStack(
2576-
task,
2577-
element._debugInfo,
2578-
);
2596+
if (enableOwnerStacks) {
2597+
previousDebugTask = task.debugTask;
2598+
}
2599+
pushServerComponentStack(task, element._debugInfo);
2600+
if (enableOwnerStacks) {
2601+
task.debugTask = debugTask = element._debugTask;
2602+
}
25792603
}
25802604

25812605
const name = getComponentNameFromType(type);
25822606
const keyOrIndex =
25832607
key == null ? (childIndex === -1 ? 0 : childIndex) : key;
25842608
const keyPath = [task.keyPath, name, keyOrIndex];
25852609
if (task.replay !== null) {
2586-
const debugTask: null | ConsoleTask =
2587-
__DEV__ && enableOwnerStacks ? element._debugTask : null;
25882610
if (debugTask) {
25892611
debugTask.run(
25902612
replayElement.bind(
@@ -2623,8 +2645,6 @@ function renderNodeDestructive(
26232645
// prelude and skip it during the replay.
26242646
} else {
26252647
// We're doing a plain render.
2626-
const debugTask: null | ConsoleTask =
2627-
__DEV__ && enableOwnerStacks ? element._debugTask : null;
26282648
if (debugTask) {
26292649
debugTask.run(
26302650
renderElement.bind(
@@ -2654,6 +2674,9 @@ function renderNodeDestructive(
26542674
}
26552675
if (__DEV__) {
26562676
task.componentStack = previousComponentStack;
2677+
if (enableOwnerStacks) {
2678+
task.debugTask = previousDebugTask;
2679+
}
26572680
}
26582681
return;
26592682
}
@@ -2665,11 +2688,12 @@ function renderNodeDestructive(
26652688
case REACT_LAZY_TYPE: {
26662689
const lazyNode: LazyComponentType<any, any> = (node: any);
26672690
const previousComponentStack = task.componentStack;
2691+
let previousDebugTask = null;
26682692
if (__DEV__) {
2669-
task.componentStack = createServerComponentStack(
2670-
task,
2671-
lazyNode._debugInfo,
2672-
);
2693+
if (enableOwnerStacks) {
2694+
previousDebugTask = task.debugTask;
2695+
}
2696+
pushServerComponentStack(task, lazyNode._debugInfo);
26732697
}
26742698
if (!__DEV__ || task.componentStack === previousComponentStack) {
26752699
// TODO: Do we really need this stack frame? We don't on the client.
@@ -2692,6 +2716,9 @@ function renderNodeDestructive(
26922716
// We restore the stack before rendering the resolved node because once the Lazy
26932717
// has resolved any future errors
26942718
task.componentStack = previousComponentStack;
2719+
if (__DEV__ && enableOwnerStacks) {
2720+
task.debugTask = previousDebugTask;
2721+
}
26952722

26962723
// Now we render the resolved node
26972724
renderNodeDestructive(request, task, resolvedNode, childIndex);
@@ -2812,10 +2839,7 @@ function renderNodeDestructive(
28122839
const thenable: Thenable<ReactNodeList> = (maybeUsable: any);
28132840
const previousComponentStack = task.componentStack;
28142841
if (__DEV__) {
2815-
task.componentStack = createServerComponentStack(
2816-
task,
2817-
thenable._debugInfo,
2818-
);
2842+
pushServerComponentStack(task, thenable._debugInfo);
28192843
}
28202844
const result = renderNodeDestructive(
28212845
request,
@@ -3058,13 +3082,14 @@ function renderChildrenArray(
30583082
): void {
30593083
const prevKeyPath = task.keyPath;
30603084
const previousComponentStack = task.componentStack;
3085+
let previousDebugTask = null;
30613086
if (__DEV__) {
3087+
if (enableOwnerStacks) {
3088+
previousDebugTask = task.debugTask;
3089+
}
30623090
// We read debugInfo from task.node instead of children because it might have been an
30633091
// unwrapped iterable so we read from the original node.
3064-
task.componentStack = createServerComponentStack(
3065-
task,
3066-
(task.node: any)._debugInfo,
3067-
);
3092+
pushServerComponentStack(task, (task.node: any)._debugInfo);
30683093
}
30693094
if (childIndex !== -1) {
30703095
task.keyPath = [task.keyPath, 'Fragment', childIndex];
@@ -3079,6 +3104,9 @@ function renderChildrenArray(
30793104
task.keyPath = prevKeyPath;
30803105
if (__DEV__) {
30813106
task.componentStack = previousComponentStack;
3107+
if (enableOwnerStacks) {
3108+
task.debugTask = previousDebugTask;
3109+
}
30823110
}
30833111
return;
30843112
}
@@ -3112,6 +3140,9 @@ function renderChildrenArray(
31123140
task.keyPath = prevKeyPath;
31133141
if (__DEV__) {
31143142
task.componentStack = previousComponentStack;
3143+
if (enableOwnerStacks) {
3144+
task.debugTask = previousDebugTask;
3145+
}
31153146
}
31163147
return;
31173148
}
@@ -3134,6 +3165,9 @@ function renderChildrenArray(
31343165
task.keyPath = prevKeyPath;
31353166
if (__DEV__) {
31363167
task.componentStack = previousComponentStack;
3168+
if (enableOwnerStacks) {
3169+
task.debugTask = previousDebugTask;
3170+
}
31373171
}
31383172
}
31393173

@@ -3371,6 +3405,7 @@ function spawnNewSuspendedReplayTask(
33713405
task.componentStack !== null ? task.componentStack.parent : null,
33723406
task.isFallback,
33733407
!disableLegacyContext ? task.legacyContext : emptyContextObject,
3408+
__DEV__ && enableOwnerStacks ? task.debugTask : null,
33743409
);
33753410

33763411
const ping = newTask.ping;
@@ -3417,6 +3452,7 @@ function spawnNewSuspendedRenderTask(
34173452
task.componentStack !== null ? task.componentStack.parent : null,
34183453
task.isFallback,
34193454
!disableLegacyContext ? task.legacyContext : emptyContextObject,
3455+
__DEV__ && enableOwnerStacks ? task.debugTask : null,
34203456
);
34213457

34223458
const ping = newTask.ping;

0 commit comments

Comments
 (0)