File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed
examples/basic/src/routes Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change @@ -1006,4 +1006,17 @@ function defineTest(f: Fixture) {
10061006 'test-browser-only: loading...' ,
10071007 )
10081008 } )
1009+
1010+ test ( 'React.cache' , async ( { page } ) => {
1011+ await page . goto ( f . url ( ) )
1012+ await waitForHydration ( page )
1013+ await page . getByRole ( 'link' , { name : 'test-react-cache' } ) . click ( )
1014+ await expect ( page . getByTestId ( 'test-react-cache-result' ) ) . toHaveText (
1015+ '(cacheFnCount = 2, nonCacheFnCount = 3)' ,
1016+ )
1017+ await page . reload ( )
1018+ await expect ( page . getByTestId ( 'test-react-cache-result' ) ) . toHaveText (
1019+ '(cacheFnCount = 4, nonCacheFnCount = 6)' ,
1020+ )
1021+ } )
10091022}
Original file line number Diff line number Diff line change 1+ import React from 'react'
2+
3+ // Note that `React.cache` doesn't have effect inside action
4+ // since it's outside of RSC render request context.
5+ // https://github.com/hi-ogawa/reproductions/tree/main/next-rsc-action-cache
6+
7+ export async function TestReactCache ( props : { url : URL } ) {
8+ if ( props . url . searchParams . has ( 'test-react-cache' ) ) {
9+ await Promise . all ( [
10+ testCacheFn ( 'test1' ) ,
11+ testCacheFn ( 'test2' ) ,
12+ testCacheFn ( 'test1' ) ,
13+ testNonCacheFn ( 'test1' ) ,
14+ testNonCacheFn ( 'test2' ) ,
15+ testNonCacheFn ( 'test1' ) ,
16+ ] )
17+ } else {
18+ cacheFnCount = 0
19+ nonCacheFnCount = 0
20+ }
21+
22+ return (
23+ < div data-testid = "test-react-cache" >
24+ < a href = "?test-react-cache" > test-react-cache</ a > { ' ' }
25+ < span data-testid = "test-react-cache-result" >
26+ (cacheFnCount = { cacheFnCount } , nonCacheFnCount = { nonCacheFnCount } )
27+ </ span >
28+ </ div >
29+ )
30+ }
31+
32+ let cacheFnCount = 0
33+ let nonCacheFnCount = 0
34+
35+ const testCacheFn = React . cache ( async ( ...args : unknown [ ] ) => {
36+ console . log ( '[cached:args]' , args )
37+ cacheFnCount ++
38+ await new Promise ( ( resolve ) => setTimeout ( resolve , 20 ) )
39+ } )
40+
41+ const testNonCacheFn = async ( ...args : unknown [ ] ) => {
42+ console . log ( '[not-cached:args]' , args )
43+ nonCacheFnCount ++
44+ await new Promise ( ( resolve ) => setTimeout ( resolve , 20 ) )
45+ }
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ import { TestTailwindClient } from './tailwind/client'
2727import { TestTailwindServer } from './tailwind/server'
2828import { TestTemporaryReference } from './temporary-reference/client'
2929import { TestUseCache } from './use-cache/server'
30+ import { TestReactCache } from './react-cache/server'
3031import { TestHydrationMismatch } from './hydration-mismatch/server'
3132import { TestBrowserOnly } from './browser-only/client'
3233import { TestTransitiveCjsClient } from './deps/transitive-cjs/client'
@@ -75,6 +76,7 @@ export function Root(props: { url: URL }) {
7576 < TestModuleInvalidationServer />
7677 < TestBrowserOnly />
7778 < TestUseCache />
79+ < TestReactCache url = { props . url } />
7880 </ body >
7981 </ html >
8082 )
You can’t perform that action at this time.
0 commit comments