Next.jsServer ActionsError Fix2026 Updated

Next.js Server Actions Not Working— Fixed [Complete Guide 2026]

Also covers: use server directive · form actions · revalidatePath · useActionState · error handling

January 20267 min readNext.js 14/15 · App Router · React 19

⚡ Common Symptoms

Error
// 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

app/actions.ts
"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.

1

Add 'use server' Directive Correctly

Root cause — wrong directive placement
2 min

The "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.

Option A — Dedicated actions file (recommended)
// 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")
}
Option B — Inline inside a Server Component
// 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.

2

Fix Server Actions in Client Components

Cannot pass functions directly to Client Components
3 min

Client Components cannot define Server Actions inline. Import them from a separate actions.ts file that has "use server" at the top.

app/actions.ts
"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 }
}
components/contact-form.tsx — Client Component
"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>
  )
}
3

Handle Form Data Correctly

FormData fields returning null
2 min

Server 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().

Correct form + action pattern
// 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 } })
}
4

Fix revalidatePath / revalidateTag Not Working

Page not refreshing after mutation
2 min

After a Server Action mutates data, call revalidatePath or revalidateTag to purge the Next.js cache for the affected route.

revalidatePath — common mistakes
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.

5

Error Handling in Server Actions

Errors crashing the page instead of showing a message
3 min

Use useActionState (React 19) to capture errors from Server Actions and display them in the form without crashing the page.

app/actions.ts — return errors, don't throw to client
"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." }
  }
}
components/register-form.tsx
"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

Frequently Asked Questions

What is a Next.js Server Action?+
A Server Action is an async function marked with 'use server' that runs exclusively on the server. You can call it directly from Client Components or use it as a form action. Next.js handles the network request automatically — no API route needed.
Why is my Server Action not running?+
The most common causes are: missing 'use server' directive, calling the action from a Server Component without it being async, passing non-serialisable arguments, or the function not being exported from a separate actions file when used in a Client Component.
Can I use a Server Action in a Client Component?+
Yes. Import the Server Action from a file that has 'use server' at the top (not inline). In the Client Component, call it like any async function. Next.js automatically handles the client-to-server bridge.
Why is revalidatePath not refreshing my page?+
revalidatePath must be called inside the Server Action after the data mutation, with the exact path string including the leading slash. If the path uses dynamic segments, you must pass the full resolved path, not the route pattern.
Can Server Actions replace API routes?+
For most data mutations (form submissions, creating/updating/deleting records), yes. Server Actions are simpler — no fetch, no route file needed. Keep API routes for webhooks, third-party callbacks, or when you need HTTP-level control over the response.
How do I handle errors in Server Actions?+
Throw errors inside the Server Action and catch them with useActionState (React 19) or a try/catch in your calling code. Never return sensitive error details to the client — log them server-side and return a user-safe message.

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