-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Segment Cache] Evict client cache on revalidate
This implements evicting the client cache when a Server Action calls revalidatePath or revalidateTag. Similar to the old prefetching implementation, it works by clearing the entire client cache, as opposed to only the affected path or tags. There are more changes needed on the server before we can support granular cache eviction. This just gets us to parity with the status quo.
- Loading branch information
Showing
10 changed files
with
387 additions
and
40 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
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
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
40 changes: 40 additions & 0 deletions
40
test/e2e/app-dir/segment-cache/revalidation/app/greeting/page.tsx
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,40 @@ | ||
'use cache' | ||
|
||
import { unstable_cacheTag as cacheTag } from 'next/cache' | ||
import { Suspense } from 'react' | ||
|
||
const TEST_DATA_SERVICE_URL = process.env.TEST_DATA_SERVICE_URL | ||
const ARTIFICIAL_DELAY = 3000 | ||
|
||
async function Greeting() { | ||
cacheTag('random-greeting') | ||
if (!TEST_DATA_SERVICE_URL) { | ||
// If environment variable is not set, resolve automatically after a delay. | ||
// This is so you can run the test app locally without spinning up a | ||
// data server. | ||
await new Promise<void>((resolve) => | ||
setTimeout(() => resolve(), ARTIFICIAL_DELAY) | ||
) | ||
// Return a random greeting | ||
return ['Hello', 'Hi', 'Hey', 'Howdy'][Math.floor(Math.random() * 4)] | ||
} | ||
const response = await fetch(TEST_DATA_SERVICE_URL + '?key=random-greeting') | ||
const text = await response.text() | ||
if (response.status !== 200) { | ||
throw new Error(text) | ||
} | ||
return ( | ||
<> | ||
<h1>Greeting</h1> | ||
<div id="greeting">{text}</div> | ||
</> | ||
) | ||
} | ||
|
||
export default async function Page() { | ||
return ( | ||
<Suspense fallback="Loading..."> | ||
<Greeting /> | ||
</Suspense> | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
test/e2e/app-dir/segment-cache/revalidation/app/layout.tsx
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,11 @@ | ||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<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,44 @@ | ||
'use cache' | ||
|
||
import { revalidatePath, revalidateTag } from 'next/cache' | ||
import { LinkAccordion } from '../components/link-accordion' | ||
import Link from 'next/link' | ||
|
||
export default async function Page() { | ||
return ( | ||
<> | ||
<form> | ||
<button | ||
id="revalidate-by-path" | ||
formAction={async function () { | ||
'use server' | ||
revalidatePath('/greeting') | ||
}} | ||
> | ||
Revalidate by path | ||
</button> | ||
<button | ||
id="revalidate-by-tag" | ||
formAction={async function () { | ||
'use server' | ||
revalidateTag('random-greeting') | ||
}} | ||
> | ||
Revalidate by tag | ||
</button> | ||
</form> | ||
<ul> | ||
<li> | ||
<LinkAccordion href="/greeting"> | ||
Link to target page with prefetching enabled | ||
</LinkAccordion> | ||
</li> | ||
<li> | ||
<Link prefetch={false} href="/greeting"> | ||
Link to target with prefetching disabled | ||
</Link> | ||
</li> | ||
</ul> | ||
</> | ||
) | ||
} |
23 changes: 23 additions & 0 deletions
23
test/e2e/app-dir/segment-cache/revalidation/components/link-accordion.tsx
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,23 @@ | ||
'use client' | ||
|
||
import Link from 'next/link' | ||
import { useState } from 'react' | ||
|
||
export function LinkAccordion({ href, children }) { | ||
const [isVisible, setIsVisible] = useState(false) | ||
return ( | ||
<> | ||
<input | ||
type="checkbox" | ||
checked={isVisible} | ||
onChange={() => setIsVisible(!isVisible)} | ||
data-link-accordion={href} | ||
/> | ||
{isVisible ? ( | ||
<Link href={href}>{children}</Link> | ||
) : ( | ||
`${children} (link is hidden)` | ||
)} | ||
</> | ||
) | ||
} |
12 changes: 12 additions & 0 deletions
12
test/e2e/app-dir/segment-cache/revalidation/next.config.js
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,12 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
experimental: { | ||
ppr: true, | ||
dynamicIO: true, | ||
clientSegmentCache: true, | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
Oops, something went wrong.