Skip to content

Commit 26ad4ad

Browse files
Copilothi-ogawa
andauthored
test(rsc): test React.cache (#668)
Co-authored-by: Hiroshi Ogawa <[email protected]>
1 parent f4e0d2a commit 26ad4ad

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

packages/plugin-rsc/e2e/basic.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

packages/plugin-rsc/examples/basic/src/routes/root.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { TestTailwindClient } from './tailwind/client'
2727
import { TestTailwindServer } from './tailwind/server'
2828
import { TestTemporaryReference } from './temporary-reference/client'
2929
import { TestUseCache } from './use-cache/server'
30+
import { TestReactCache } from './react-cache/server'
3031
import { TestHydrationMismatch } from './hydration-mismatch/server'
3132
import { TestBrowserOnly } from './browser-only/client'
3233
import { 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
)

0 commit comments

Comments
 (0)