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) {
1006
1006
'test-browser-only: loading...' ,
1007
1007
)
1008
1008
} )
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
+ } )
1009
1022
}
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'
27
27
import { TestTailwindServer } from './tailwind/server'
28
28
import { TestTemporaryReference } from './temporary-reference/client'
29
29
import { TestUseCache } from './use-cache/server'
30
+ import { TestReactCache } from './react-cache/server'
30
31
import { TestHydrationMismatch } from './hydration-mismatch/server'
31
32
import { TestBrowserOnly } from './browser-only/client'
32
33
import { TestTransitiveCjsClient } from './deps/transitive-cjs/client'
@@ -75,6 +76,7 @@ export function Root(props: { url: URL }) {
75
76
< TestModuleInvalidationServer />
76
77
< TestBrowserOnly />
77
78
< TestUseCache />
79
+ < TestReactCache url = { props . url } />
78
80
</ body >
79
81
</ html >
80
82
)
You can’t perform that action at this time.
0 commit comments