-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revalidate APIs should make route handlers dynamic (#58466)
### What? Using `revalidateTag` or `revalidatePath` in a route handler will not currently opt the handler into dynamic behavior. This means that if you use these APIs and don't opt into dynamic behavior by some other means, the revalidation call won't do anything as the route handler will be served statically. ### Why? During static generation, we do not currently indicate that usage of these APIs should opt into dynamic usage. ### How? This updates `revalidateTag` to throw a `DynamicUsageError` (similar to our other scenarios, such as search params bailout, headers/cookies, or fetch + revalidate/no-store) Closes NEXT-1712
- Loading branch information
Showing
6 changed files
with
80 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
7 changes: 7 additions & 0 deletions
7
test/e2e/app-dir/revalidate-dynamic/app/api/revalidate-path/route.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,7 @@ | ||
import { NextResponse } from 'next/server' | ||
import { revalidatePath } from 'next/cache' | ||
|
||
export async function GET(req) { | ||
revalidatePath('/') | ||
return NextResponse.json({ revalidated: true, now: Date.now() }) | ||
} |
7 changes: 7 additions & 0 deletions
7
test/e2e/app-dir/revalidate-dynamic/app/api/revalidate-tag/route.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,7 @@ | ||
import { NextResponse } from 'next/server' | ||
import { revalidateTag } from 'next/cache' | ||
|
||
export async function GET(req) { | ||
revalidateTag('thankyounext') | ||
return NextResponse.json({ revalidated: true, now: Date.now() }) | ||
} |
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 Layout({ children }) { | ||
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,17 @@ | ||
export default async function Page() { | ||
const data = await fetch( | ||
'https://next-data-api-endpoint.vercel.app/api/random', | ||
{ | ||
next: { | ||
tags: ['thankyounext'], | ||
}, | ||
} | ||
).then((res) => res.text()) | ||
|
||
return ( | ||
<> | ||
Data: | ||
<div id="data-value">{data}</div> | ||
</> | ||
) | ||
} |
36 changes: 36 additions & 0 deletions
36
test/e2e/app-dir/revalidate-dynamic/revalidate-dynamic.test.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,36 @@ | ||
import { createNextDescribe } from '../../../lib/e2e-utils' | ||
|
||
createNextDescribe( | ||
'app-dir revalidate-dynamic', | ||
{ | ||
files: __dirname, | ||
}, | ||
({ next, isNextStart }) => { | ||
if (isNextStart) { | ||
it('should correctly mark a route handler that uses revalidateTag as dynamic', async () => { | ||
expect(next.cliOutput).toContain('λ /api/revalidate-path') | ||
expect(next.cliOutput).toContain('λ /api/revalidate-tag') | ||
}) | ||
} | ||
|
||
it.each(['/api/revalidate-path', '/api/revalidate-tag'])( | ||
`should revalidate the data with %s`, | ||
async (path) => { | ||
const browser = await next.browser('/') | ||
const randomNumber = await browser.elementById('data-value').text() | ||
await browser.refresh() | ||
const randomNumber2 = await browser.elementById('data-value').text() | ||
|
||
expect(randomNumber).toEqual(randomNumber2) | ||
|
||
const revalidateRes = await next.fetch(path) | ||
expect((await revalidateRes.json()).revalidated).toBe(true) | ||
|
||
await browser.refresh() | ||
|
||
const randomNumber3 = await browser.elementById('data-value').text() | ||
expect(randomNumber).not.toEqual(randomNumber3) | ||
} | ||
) | ||
} | ||
) |