Skip to content

Commit 8d4f4fc

Browse files
authored
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
1 parent 02103fe commit 8d4f4fc

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

packages/next/src/server/web/spec-extension/revalidate-tag.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
StaticGenerationAsyncStorage,
33
StaticGenerationStore,
44
} from '../../../client/components/static-generation-async-storage.external'
5+
import { staticGenerationBailout } from '../../../client/components/static-generation-bailout'
56

67
export function revalidateTag(tag: string) {
78
const staticGenerationAsyncStorage = (
@@ -16,6 +17,11 @@ export function revalidateTag(tag: string) {
1617
`Invariant: static generation store missing in revalidateTag ${tag}`
1718
)
1819
}
20+
21+
// a route that makes use of revalidation APIs should be considered dynamic
22+
// as otherwise it would be impossible to revalidate
23+
staticGenerationBailout(`revalidateTag ${tag}`)
24+
1925
if (!store.revalidatedTags) {
2026
store.revalidatedTags = []
2127
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { NextResponse } from 'next/server'
2+
import { revalidatePath } from 'next/cache'
3+
4+
export async function GET(req) {
5+
revalidatePath('/')
6+
return NextResponse.json({ revalidated: true, now: Date.now() })
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { NextResponse } from 'next/server'
2+
import { revalidateTag } from 'next/cache'
3+
4+
export async function GET(req) {
5+
revalidateTag('thankyounext')
6+
return NextResponse.json({ revalidated: true, now: Date.now() })
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Layout({ children }) {
2+
return (
3+
<html lang="en">
4+
<body>{children}</body>
5+
</html>
6+
)
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default async function Page() {
2+
const data = await fetch(
3+
'https://next-data-api-endpoint.vercel.app/api/random',
4+
{
5+
next: {
6+
tags: ['thankyounext'],
7+
},
8+
}
9+
).then((res) => res.text())
10+
11+
return (
12+
<>
13+
Data:
14+
<div id="data-value">{data}</div>
15+
</>
16+
)
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { createNextDescribe } from '../../../lib/e2e-utils'
2+
3+
createNextDescribe(
4+
'app-dir revalidate-dynamic',
5+
{
6+
files: __dirname,
7+
},
8+
({ next, isNextStart }) => {
9+
if (isNextStart) {
10+
it('should correctly mark a route handler that uses revalidateTag as dynamic', async () => {
11+
expect(next.cliOutput).toContain('λ /api/revalidate-path')
12+
expect(next.cliOutput).toContain('λ /api/revalidate-tag')
13+
})
14+
}
15+
16+
it.each(['/api/revalidate-path', '/api/revalidate-tag'])(
17+
`should revalidate the data with %s`,
18+
async (path) => {
19+
const browser = await next.browser('/')
20+
const randomNumber = await browser.elementById('data-value').text()
21+
await browser.refresh()
22+
const randomNumber2 = await browser.elementById('data-value').text()
23+
24+
expect(randomNumber).toEqual(randomNumber2)
25+
26+
const revalidateRes = await next.fetch(path)
27+
expect((await revalidateRes.json()).revalidated).toBe(true)
28+
29+
await browser.refresh()
30+
31+
const randomNumber3 = await browser.elementById('data-value').text()
32+
expect(randomNumber).not.toEqual(randomNumber3)
33+
}
34+
)
35+
}
36+
)

0 commit comments

Comments
 (0)