Skip to content
Learn Netverks

Lesson

Step 24/36 67% through track

server-actions

Server Actions

Last reviewed May 28, 2026 Content v20260528
Track mode
client_nextjs
Means
In-browser Next.js (client components)
Reading
~2 min
Level
advanced

This lesson

This lesson teaches Server Actions: the concepts, APIs, and habits you need before advancing in Next.js.

Without Server Actions, you will struggle to read or extend Next.js codebases and playground exercises.

You will apply Server Actions in contexts like: Marketing sites, dashboards, e-commerce, and Vercel-style deployments that need hybrid static + dynamic pages.

Write TSX for Client Components, click Run—React 18 CDN + in-browser TSX compile; use client/server lessons explain App Router concepts; mountApp renders interactive UI; printOutput feeds the terminal. Also read server-component lessons carefully—SSR runs in a real Next project, not in the iframe.

When hooks, state, and effects from intermediate lessons are familiar.

Server Actions are async functions that run on the server, invoked from forms or client event handlers. Mark a function with 'use server' (at file or function level) to mutate data without hand-writing fetch boilerplate.

Form action pattern

// app/actions.ts
'use server';
export async function createTodo(formData: FormData) {
  const title = formData.get('title') as string;
  await db.todo.create({ data: { title } });
  revalidatePath('/todos');
}

// app/todos/page.tsx
import { createTodo } from '../actions';

export default function Page() {
  return (
    <form action={createTodo}>
      <input name="title" />
      <button type="submit">Add</button>
    </form>
  );
}

Progressive enhancement

Forms work without JavaScript until you add client enhancements—good baseline for accessibility and resilience.

Security

Always validate and authorize on the server. Treat action inputs like any API payload—never trust the client.

Important interview questions and answers

  1. Q: Server Action vs API route?
    A: Actions integrate with React forms and RSC; Route Handlers expose HTTP for arbitrary clients.
  2. Q: Where does 'use server' go?
    A: Top of a dedicated actions file or inline above the async function.

Self-check

  1. Why revalidate after a mutation?
  2. What type is formData in an action?

Challenge

Validate the action

  1. Extend the simulated action to reject empty titles.
  2. Print the error shape when validation fails.

Done when: terminal shows ok:false when title is blank.

Interview prep

Server Action vs Route Handler?

Actions integrate with forms and RSC mutations; Route Handlers expose HTTP endpoints for webhooks and non-React clients.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Progressive enhancement?
  • revalidatePath when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump