Files
Zack TannerandJiachi Liu dd44191118 fix root page revalidation when redirecting in a server action (#64730)
When a server action triggers a redirect, we're incorrectly applying a
refresh marker to the segment they were on, rather than the segment they
were being redirected to. As a result, when revalidation occurs (via
`revalidateX` or `router.refresh()`), the top-level segment would be
replaced with data for an incorrect segment.

For example, if triggering a redirect action from `/redirect` to `/`,
the router state tree would save a reference to `/redirect`. The next
time a refresh or revalidate happens, it'd refresh the segment data for
`/redirect` instead of `/`.

Fixes #64728


Closes NEXT-3156
2024-04-23 17:34:14 +02:00

30 lines
619 B
TypeScript

'use server'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
let data = []
export async function addData(newData: string) {
// sleep 1s
await new Promise((resolve) => setTimeout(resolve, 1000))
data.push(newData)
}
export async function getData() {
// sleep 1s
await new Promise((resolve) => setTimeout(resolve, 1000))
return data
}
export async function redirectAction() {
'use server'
console.log('redirecting...')
await new Promise((res) => setTimeout(res, 1000))
redirect('/')
}
export async function clearData() {
data = []
revalidatePath('/')
}