-
Notifications
You must be signed in to change notification settings - Fork 26k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: added test for inner unstable cache
- Loading branch information
Showing
2 changed files
with
134 additions
and
15 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
49 changes: 49 additions & 0 deletions
49
test/e2e/app-dir/app-static/app/unstable-cache/fetch/route.ts
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,49 @@ | ||
import { revalidateTag, unstable_cache } from 'next/cache' | ||
|
||
export const dynamic = 'force-dynamic' | ||
|
||
const getData = unstable_cache( | ||
async (address: string): Promise<string> => { | ||
const res = await fetch(address, { | ||
cache: 'force-cache', | ||
next: { tags: ['unstable-cache-inner-fetch'] }, | ||
}) | ||
|
||
const data = await res.text() | ||
|
||
return JSON.stringify({ | ||
random: Math.floor(Math.random() * 100).toString(), | ||
data, | ||
}) | ||
}, | ||
undefined, | ||
{ | ||
tags: ['unstable-cache-fetch'], | ||
} | ||
) | ||
|
||
export const GET = async (request: Request): Promise<Response> => { | ||
const address = request.headers.get('x-test-data-server') | ||
if (!address) { | ||
return new Response('Missing address', { status: 400 }) | ||
} | ||
|
||
const data = await getData(address) | ||
|
||
return new Response(data, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
} | ||
|
||
export const DELETE = async (request: Request): Promise<Response> => { | ||
const url = new URL(request.url) | ||
const tags = url.searchParams.getAll('tag') | ||
|
||
for (const tag of tags) { | ||
revalidateTag(tag) | ||
} | ||
|
||
return new Response('OK', { status: 200 }) | ||
} |