Skip to content

Commit 78d60d9

Browse files
committed
Cache parsed stacks
Since we can only trigger prepareStackFrame once, it's important to cache this since the second time it gets accessed we won't be able to get to it.
1 parent 72bc9a6 commit 78d60d9

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

packages/react-server/src/ReactFlightServer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,20 @@ function defaultFilterStackFrame(
152152
);
153153
}
154154

155+
// DEV-only cache of parsed and filtered stack frames.
156+
const stackTraceCache: WeakMap<Error, ReactStackTrace> = __DEV__
157+
? new WeakMap()
158+
: (null: any);
159+
155160
function filterStackTrace(
156161
request: Request,
157162
error: Error,
158163
skipFrames: number,
159164
): ReactStackTrace {
165+
const existing = stackTraceCache.get(error);
166+
if (existing !== undefined) {
167+
return existing;
168+
}
160169
// Since stacks can be quite large and we pass a lot of them, we filter them out eagerly
161170
// to save bandwidth even in DEV. We'll also replay these stacks on the client so by
162171
// stripping them early we avoid that overhead. Otherwise we'd normally just rely on
@@ -183,6 +192,7 @@ function filterStackTrace(
183192
i--;
184193
}
185194
}
195+
stackTraceCache.set(error, stack);
186196
return stack;
187197
}
188198

0 commit comments

Comments
 (0)