Next.js Server Actions Not Working— Fixed [Complete Guide 2026]
Also covers: use server directive · form actions · revalidatePath · useActionState · error handling
⚡ Common Symptoms
// Server Action silently does nothing
// OR:
Error: Server Actions must be async functions.
// OR:
Error: Functions cannot be passed directly to Client Components
unless you explicitly expose it by marking it with "use server".✅ Correct Pattern
"use server" // ← top of the file, not inside the function
export async function createPost(formData: FormData) {
const title = formData.get("title") as string
await db.post.create({ data: { title } })
revalidatePath("/posts")
}Why Server Actions Fail in Next.js
Server Actions were introduced as stable in Next.js 14 and enhanced further in Next.js 15 with React 19. They fail silently or throw cryptic errors when the "use server" directive is misplaced, when non-serialisable data is passed as arguments, or when revalidatePath is called outside the action scope.
Add 'use server' Directive Correctly
Root cause — wrong directive placementThe "use server" directive can go either at the top of a dedicated actions file (recommended) or inline at the top of an individual async function. The inline form is only valid inside Server Components.
// app/actions.ts — "use server" at file level
"use server"
import { db } from "@/lib/db"
import { revalidatePath } from "next/cache"
export async function createPost(formData: FormData) {
const title = formData.get("title") as string
await db.post.create({ data: { title } })
revalidatePath("/posts")
}
export async function deletePost(id: string) {
await db.post.delete({ where: { id } })
revalidatePath("/posts")
}// app/posts/page.tsx — Server Component only
export default function PostsPage() {
async function createPost(formData: FormData) {
"use server" // ← inline inside function, Server Component only
const title = formData.get("title") as string
await db.post.create({ data: { title } })
revalidatePath("/posts")
}
return <form action={createPost}><input name="title" /><button>Create</button></form>
}Inline 'use server' inside a function only works in Server Components. In Client Components, always import from a dedicated actions file.
Fix Server Actions in Client Components
Cannot pass functions directly to Client ComponentsClient Components cannot define Server Actions inline. Import them from a separate actions.ts file that has "use server" at the top.
"use server"
import { db } from "@/lib/db"
import { revalidatePath } from "next/cache"
export async function submitContact(formData: FormData) {
const email = formData.get("email") as string
const message = formData.get("message") as string
await db.contact.create({ data: { email, message } })
revalidatePath("/contact")
return { success: true }
}"use client"
import { useActionState } from "react"
import { submitContact } from "@/app/actions"
export function ContactForm() {
const [state, formAction, isPending] = useActionState(submitContact, null)
return (
<form action={formAction}>
<input name="email" type="email" required />
<textarea name="message" required />
{state?.success && <p>Message sent!</p>}
<button disabled={isPending}>
{isPending ? "Sending..." : "Send Message"}
</button>
</form>
)
}Handle Form Data Correctly
FormData fields returning nullServer Actions receive a FormData object when used as a form action. Every input must have a name attribute, and values must be extracted with formData.get().
// Form — every input needs name=""
<form action={createProduct}>
<input name="title" /> {/* ✅ has name */}
<input name="price" /> {/* ✅ has name */}
<input /> {/* ❌ no name — formData.get() returns null */}
<button type="submit">Create</button>
</form>
// Server Action — extract with .get() and cast type
async function createProduct(formData: FormData) {
"use server"
const title = formData.get("title") as string // string
const price = Number(formData.get("price")) // number
const image = formData.get("image") as File | null // file upload
if (!title) throw new Error("Title is required")
await db.product.create({ data: { title, price } })
}Fix revalidatePath / revalidateTag Not Working
Page not refreshing after mutationAfter a Server Action mutates data, call revalidatePath or revalidateTag to purge the Next.js cache for the affected route.
import { revalidatePath, revalidateTag } from "next/cache"
async function updatePost(id: string, formData: FormData) {
"use server"
await db.post.update({ where: { id }, data: { title: formData.get("title") as string } })
// ❌ Wrong — pattern string, not resolved path
revalidatePath("/posts/[id]")
// ✅ Correct — pass the actual resolved path
revalidatePath(`/posts/${id}`)
// ✅ Also revalidate the list page
revalidatePath("/posts")
// ✅ Or use a tag (more flexible)
revalidateTag("posts")
}Tag-based revalidation is more flexible — tag your fetches with revalidateTag, then call revalidateTag('posts') in any action that affects posts, without needing to know the exact URL.
Error Handling in Server Actions
Errors crashing the page instead of showing a messageUse useActionState (React 19) to capture errors from Server Actions and display them in the form without crashing the page.
"use server"
type ActionState = { error?: string; success?: boolean }
export async function registerUser(
prevState: ActionState,
formData: FormData
): Promise<ActionState> {
try {
const email = formData.get("email") as string
const existing = await db.user.findUnique({ where: { email } })
if (existing) return { error: "Email already registered" }
await db.user.create({ data: { email } })
return { success: true }
} catch (err) {
console.error("registerUser error:", err)
return { error: "Something went wrong. Please try again." }
}
}"use client"
import { useActionState } from "react"
import { registerUser } from "@/app/actions"
export function RegisterForm() {
const [state, formAction, isPending] = useActionState(registerUser, {})
return (
<form action={formAction}>
<input name="email" type="email" />
{state.error && <p className="text-red-600">{state.error}</p>}
{state.success && <p className="text-green-600">Registered!</p>}
<button disabled={isPending}>{isPending ? "Registering..." : "Register"}</button>
</form>
)
}Prevention
- Always put Server Actions in a dedicated actions.ts file with 'use server' at the top
- Every form input must have a name attribute — formData.get() returns null without it
- Call revalidatePath with the exact resolved URL, not the route pattern
- Return error objects from Server Actions instead of throwing to avoid unhandled rejections
- Use useActionState (React 19) in Client Components for pending state and error display
- Never pass sensitive data or secrets back in the Server Action return value
Frequently Asked Questions
What is a Next.js Server Action?+−
Why is my Server Action not running?+−
Can I use a Server Action in a Client Component?+−
Why is revalidatePath not refreshing my page?+−
Can Server Actions replace API routes?+−
How do I handle errors in Server Actions?+−
Need Expert Help?
We Build Production Next.js Applications
Softplix engineers build full-stack Next.js apps with Server Actions, Prisma, and Supabase. Need help with your App Router architecture? Let us help.
Talk to an Engineer