Server Actions — a Next.js App Router feature (stable since Next 14, 2023): async functions on the server callable directly from React components (form or button). No need to write API routes. Marked with a "use server" directive. Progressive enhancement: work without JS via native form submission. Replaces the API-route + client-side fetch pattern for mutations.
Below: details, example, related terms, FAQ.
Free online tool — page speed test: instant results, no signup.
"use server" at the top of an async function<form action={submitAction}> — works without JS"use server";
export async function createPost(formData) {
const session = await auth();
if (!session) throw new Error('Unauthorized');
await db.post.create({ data: { title: formData.get('title') } });
revalidatePath('/posts');
}Server Actions are a powerful feature introduced in Next.js 14, allowing developers to create asynchronous functions on the server that can be invoked directly from React components. This eliminates the need to create separate API routes for handling data mutations, streamlining the development process.
To define a Server Action, you simply create an asynchronous function in a component file and annotate it with the "use server" directive. This directive indicates that the function should run on the server rather than the client. For example:
async function handleSubmit(data) { /* processing logic */ }By using Server Actions, developers can leverage progressive enhancement techniques, as these actions will function even if JavaScript is disabled in the user's browser, thanks to native form submissions.
Server Actions also enhance performance by reducing the amount of client-side code, leading to faster load times and improved user experiences. Since the server handles the logic, it can be optimized for speed and efficiency.
Implementing Server Actions in Next.js involves a few straightforward steps. Below are practical examples that demonstrate how to set up and utilize Server Actions in your application.
1. **Define a Server Action:** Create a new file, for example, app/actions.js, and define your Server Action:
export async function saveData(formData) { const response = await fetch('/api/save', { method: 'POST', body: formData }); return response.json(); }2. **Use the Action in a Component:** In your component, you can call this action directly:
import { saveData } from './actions';3. **Form Implementation:** Create a form that utilizes the Server Action:
<form action={saveData} method="POST"> <input type="text" name="data" /> <button type="submit">Submit</button> </form>This setup allows you to submit the form directly to the Server Action, providing a seamless experience. The action can handle data processing and responding without needing to manage API routes.
Server Actions present several advantages over traditional API routes in Next.js applications. Understanding these benefits can help developers make informed decisions regarding architecture and performance optimizations.
In summary, Server Actions offer a modern approach to handling server-side logic in Next.js applications, making them a preferred choice for many developers looking to enhance their workflow and application performance.
Page load speed directly impacts conversion, SEO rankings, and user satisfaction. Google uses Core Web Vitals as a ranking factor. Every extra second of load time cancost up to 7% in conversions.
Google Lighthouse-based analysis: Performance, Accessibility, Best Practices, SEO.
LCP (rendering), FID (interactivity), CLS (visual stability) — key Google metrics.
Breakdown by type: HTML, CSS, JavaScript, images, fonts. Size, request count, blocking resources.
Specific recommendations with savings estimates: image compression, caching, minification, etc.
Core Web Vitals for rankings
performance optimization
speed = conversions
performance regression
async/defer block rendering. Move to end or add attribute.Cache-Control, the browser reloads CSS/JS on every visit.loading="lazy" for images below the fold.brotli on;Cache-Control: max-age=31536000, immutable. HTML: max-age=0, s-maxage=60.<link rel="preload"> for fonts and CSS. Reduces LCP by 200-500ms.Speed check history, competitor comparison and PageSpeed monitoring.
Sign up freeServer Actions — short-lived, called from a component, type-safe, RPC-like. API routes — public endpoints for external clients.
You must re-validate auth + input inside the action. Next.js adds CSRF protection automatically. Always check ownership (IDOR).
Yes! If form action is a Server Action, submit works via native form POST without JS. Progressive enhancement.
Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.