nextjs runtime_error ai_generated true

Error: Server Action redirected but the page was not revalidated. Use revalidatePath or revalidateTag before redirect to ensure fresh data.

ID: nextjs/server-action-redirect-without-revalidate

Also available as: JSON · Markdown · 中文
88%Fix Rate
83%Confidence
1Evidence
2024-08-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Next.js 14.x active
Next.js 15.x active

Root Cause

A Server Action calls redirect() after mutating data without calling revalidatePath() or revalidateTag() first. Next.js requires explicit cache revalidation before redirects in Server Actions to prevent stale data from being displayed.

generic

中文

服务端操作在修改数据后调用了 redirect(),但未先调用 revalidatePath() 或 revalidateTag()。Next.js 要求服务端操作在重定向前进行显式缓存重新验证,以防止显示过时数据。

Official Documentation

https://nextjs.org/docs/app/api-reference/functions/server-actions#redirecting

Workarounds

  1. 88% success Call revalidatePath() before redirect() in your Server Action. Example: 'use server'; import { revalidatePath } from 'next/cache'; import { redirect } from 'next/navigation'; export async function updateData(formData) { // mutate data revalidatePath('/dashboard'); redirect('/dashboard'); }
    Call revalidatePath() before redirect() in your Server Action. Example: 'use server'; import { revalidatePath } from 'next/cache'; import { redirect } from 'next/navigation'; export async function updateData(formData) { // mutate data revalidatePath('/dashboard'); redirect('/dashboard'); }
  2. 85% success Use revalidateTag() if you have tagged your fetch calls. Example: revalidateTag('posts'); redirect('/posts');
    Use revalidateTag() if you have tagged your fetch calls. Example: revalidateTag('posts'); redirect('/posts');
  3. 75% success If you do not want to redirect, return a success response instead and let the client handle navigation with router.push() after the Server Action completes.
    If you do not want to redirect, return a success response instead and let the client handle navigation with router.push() after the Server Action completes.

中文步骤

  1. 在服务端操作中的 redirect() 之前调用 revalidatePath()。示例:'use server'; import { revalidatePath } from 'next/cache'; import { redirect } from 'next/navigation'; export async function updateData(formData) { // 修改数据 revalidatePath('/dashboard'); redirect('/dashboard'); }
  2. 如果您的 fetch 调用已添加标签,请使用 revalidateTag()。示例:revalidateTag('posts'); redirect('/posts');
  3. 如果不想重定向,请返回成功响应,让客户端在服务端操作完成后使用 router.push() 处理导航。

Dead Ends

Common approaches that don't work:

  1. 100% fail

    redirect() throws a NEXT_REDIRECT error that stops execution. Any code after redirect() will never run. revalidation must happen before the redirect call.

  2. 95% fail

    router.refresh() is a client-side method and cannot be called inside a Server Action. It also does not affect server-side cache invalidation.

  3. 60% fail

    This prevents caching but does not revalidate the specific path after mutation. The redirect will still work, but Next.js will still warn about missing revalidation, and the behavior is not guaranteed in future versions.