File tree Expand file tree Collapse file tree 4 files changed +67
-0
lines changed
test/e2e/app-dir/use-cache Expand file tree Collapse file tree 4 files changed +67
-0
lines changed Original file line number Diff line number Diff line change 1+ export default function Root ( { children } : { children : React . ReactNode } ) {
2+ return (
3+ < html >
4+ < body > { children } </ body >
5+ </ html >
6+ )
7+ }
Original file line number Diff line number Diff line change 1+ async function getCachedRandom ( x : number ) {
2+ 'use cache'
3+ return {
4+ x,
5+ y : Math . random ( ) ,
6+ }
7+ }
8+
9+ export default async function Page ( {
10+ searchParams,
11+ } : {
12+ searchParams : Promise < { n : string } >
13+ } ) {
14+ const n = + ( await searchParams ) . n
15+ const values = await getCachedRandom ( n )
16+ return (
17+ < >
18+ < p id = "x" > { values . x } </ p >
19+ < p id = "y" > { values . y } </ p >
20+ </ >
21+ )
22+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * @type {import('next').NextConfig }
3+ */
4+ const nextConfig = {
5+ experimental : {
6+ dynamicIO : true ,
7+ } ,
8+ }
9+
10+ module . exports = nextConfig
Original file line number Diff line number Diff line change 1+ // @ts -check
2+ import { nextTestSetup } from 'e2e-utils'
3+
4+ describe ( 'use-cache' , ( ) => {
5+ const { next } = nextTestSetup ( {
6+ files : __dirname ,
7+ } )
8+
9+ it ( 'should cache results' , async ( ) => {
10+ const browser = await next . browser ( '/?n=1' )
11+ expect ( await browser . waitForElementByCss ( '#x' ) . text ( ) ) . toBe ( '1' )
12+ const random1a = await browser . waitForElementByCss ( '#y' ) . text ( )
13+
14+ await browser . loadPage ( new URL ( '/?n=2' , next . url ) . toString ( ) )
15+ expect ( await browser . waitForElementByCss ( '#x' ) . text ( ) ) . toBe ( '2' )
16+ const random2 = await browser . waitForElementByCss ( '#y' ) . text ( )
17+
18+ await browser . loadPage ( new URL ( '/?n=1&unrelated' , next . url ) . toString ( ) )
19+ expect ( await browser . waitForElementByCss ( '#x' ) . text ( ) ) . toBe ( '1' )
20+ const random1b = await browser . waitForElementByCss ( '#y' ) . text ( )
21+
22+ // The two navigations to n=1 should use a cached value.
23+ expect ( random1a ) . toBe ( random1b )
24+
25+ // The navigation to n=2 should be some other random value.
26+ expect ( random1a ) . not . toBe ( random2 )
27+ } )
28+ } )
You can’t perform that action at this time.
0 commit comments