forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42ae07f
commit 1d25312
Showing
4 changed files
with
67 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Root({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
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,22 @@ | ||
async function getCachedRandom(x: number) { | ||
'use cache' | ||
return { | ||
x, | ||
y: Math.random(), | ||
} | ||
} | ||
|
||
export default async function Page({ | ||
searchParams, | ||
}: { | ||
searchParams: Promise<{ n: string }> | ||
}) { | ||
const n = +(await searchParams).n | ||
const values = await getCachedRandom(n) | ||
return ( | ||
<> | ||
<p id="x">{values.x}</p> | ||
<p id="y">{values.y}</p> | ||
</> | ||
) | ||
} |
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,10 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
experimental: { | ||
dynamicIO: true, | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
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,28 @@ | ||
// @ts-check | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
describe('use-cache', () => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
it('should cache results', async () => { | ||
const browser = await next.browser('/?n=1') | ||
expect(await browser.waitForElementByCss('#x').text()).toBe('1') | ||
const random1a = await browser.waitForElementByCss('#y').text() | ||
|
||
await browser.loadPage(new URL('/?n=2', next.url).toString()) | ||
expect(await browser.waitForElementByCss('#x').text()).toBe('2') | ||
const random2 = await browser.waitForElementByCss('#y').text() | ||
|
||
await browser.loadPage(new URL('/?n=1&unrelated', next.url).toString()) | ||
expect(await browser.waitForElementByCss('#x').text()).toBe('1') | ||
const random1b = await browser.waitForElementByCss('#y').text() | ||
|
||
// The two navigations to n=1 should use a cached value. | ||
expect(random1a).toBe(random1b) | ||
|
||
// The navigation to n=2 should be some other random value. | ||
expect(random1a).not.toBe(random2) | ||
}) | ||
}) |