-
Notifications
You must be signed in to change notification settings - Fork 27.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix console replaying and
React.cache
usage in "use cache"
functi…
…ons (#75520) Console logs in server components are replayed in the browser. For example, when you run `console.log('foo')`, the log will be replayed in the browser as `[Server] foo`. When the component is inside of a `"use cache"` scope, it's replayed as `[Cache] foo`. However, when logging directly in the function body of a `"use cache"` function, we are currently not replaying the log in the browser. The reason for that is that the function is called outside of React's rendering, before handing the result promise over to React for serialization. Since the function is called without React's request storage, no console chunks are emitted. We can work around this by invoking the function lazily when React calls `.then()` on the promise. This ensures that the function is run inside of React's request storage and console chunks can be emitted. In addition, this also unlocks that `React.cache` can be used in a `"use cache"` function to dedupe other function calls. closes NAR-83
- Loading branch information
1 parent
b0706c3
commit b145593
Showing
5 changed files
with
168 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
test/e2e/app-dir/use-cache/app/(no-suspense)/logs/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
async function Bar() { | ||
'use cache' | ||
const date = new Date().toLocaleTimeString() | ||
console.log('deep inside', date) | ||
return <p>{date}</p> | ||
} | ||
|
||
async function Foo() { | ||
'use cache' | ||
console.log('inside') | ||
return <Bar /> | ||
} | ||
|
||
export default async function Page() { | ||
console.log('outside') | ||
|
||
return <Foo /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters