next.js
6541ab5b - Improve error stacks for dynamic API usage in `"use cache"` (#92736)

Commit
16 days ago
Improve error stacks for dynamic API usage in `"use cache"` (#92736) When `cookies()`, `headers()`, `draftMode()`, or `connection()` are called inside `"use cache"` from third-party (ignore-listed) code, the error stacks previously had no usable frames. The redbox showed no source location and an empty call stack, and the build output only showed `at ignore-listed frames`. This made it difficult to trace which component was responsible for the invalid API usage. This PR fixes the issue by calling React's `captureOwnerStack()` at the `"use cache"` boundary (in `use-cache-wrapper.ts`) before entering the cache scope, and storing the result on the cache work unit store as `outerOwnerStack`. When an error is later thrown inside the cache scope (e.g. from `cookies()`), `applyOwnerStack()` concatenates the inner owner stack (from within the cache scope) with the stored outer owner stack to reconstruct the full component tree across cache boundaries. This also correctly handles nested `"use cache"` scopes by chaining the outer owner stacks. The `applyOwnerStack` function was extracted from `io-utils.tsx` into `dynamic-rendering-utils.ts` so it can be reused by both the request API files (`cookies.ts`, `headers.ts`, `draft-mode.ts`, `connection.ts`) and the sync IO error handling in `io-utils.tsx`. New test cases cover both first-party and third-party usage of all four APIs, including a previously missing `connection()` in `"use cache"` test case.
Author
Parents
Loading