Google Antigravity Directory

The #1 directory for Google Antigravity prompts, rules, workflows & MCP servers. Optimized for Gemini 3 agentic development.

Resources

PromptsMCP ServersAntigravity RulesGEMINI.md GuideBest Practices

Company

Submit PromptAntigravityAI.directory

Popular Prompts

Next.js 14 App RouterReact TypeScriptTypeScript AdvancedFastAPI GuideDocker Best Practices

Legal

Privacy PolicyTerms of ServiceContact Us
Featured on FazierFeatured on WayfindioAntigravity AI - Featured on Startup FameFeatured on Wired BusinessFeatured on Twelve ToolsListed on Turbo0Featured on findly.toolsFeatured on Aura++That App ShowAI ToolzShinyLaunchMillion Dot HomepageSolver ToolsFeatured on FazierFeatured on WayfindioAntigravity AI - Featured on Startup FameFeatured on Wired BusinessFeatured on Twelve ToolsListed on Turbo0Featured on findly.toolsFeatured on Aura++That App ShowAI ToolzShinyLaunchMillion Dot HomepageSolver Tools

© 2026 Antigravity AI Directory. All rights reserved.

The #1 directory for Google Antigravity IDE

This website is not affiliated with, endorsed by, or associated with Google LLC. "Google" and "Gemini" are trademarks of Google LLC.

Antigravity AI Directory
PromptsMCPBest PracticesUse CasesLearn
Home
Prompts
Next.js Server Actions Patterns

Next.js Server Actions Patterns

Master Next.js Server Actions patterns for Google Antigravity IDE full-stack development

Next.jsServer ActionsFull StackReact
by Antigravity AI
⭐0Stars
.antigravity
# Next.js Server Actions Patterns for Google Antigravity IDE

Build full-stack applications with Server Actions using Google Antigravity IDE. This guide covers mutation patterns, optimistic updates, and error handling.

## Basic Server Action

```typescript
// src/app/actions/posts.ts
"use server";

import { revalidatePath, revalidateTag } from "next/cache";
import { redirect } from "next/navigation";
import { z } from "zod";
import { auth } from "@/lib/auth";
import { db } from "@/lib/db";

const createPostSchema = z.object({
  title: z.string().min(5).max(200),
  content: z.string().min(100),
  published: z.boolean().default(false),
});

export async function createPost(formData: FormData) {
  const session = await auth();
  if (!session?.user) {
    throw new Error("Unauthorized");
  }

  const rawData = {
    title: formData.get("title"),
    content: formData.get("content"),
    published: formData.get("published") === "on",
  };

  const validated = createPostSchema.parse(rawData);

  const post = await db.post.create({
    data: {
      ...validated,
      authorId: session.user.id,
      slug: slugify(validated.title),
    },
  });

  revalidatePath("/dashboard/posts");
  revalidateTag("posts");
  redirect("/posts/" + post.slug);
}

export async function deletePost(postId: string) {
  const session = await auth();
  if (!session?.user) {
    throw new Error("Unauthorized");
  }

  const post = await db.post.findUnique({ where: { id: postId } });

  if (!post || post.authorId !== session.user.id) {
    throw new Error("Not found or unauthorized");
  }

  await db.post.delete({ where: { id: postId } });

  revalidatePath("/dashboard/posts");
  revalidateTag("posts");
}

export async function togglePublish(postId: string) {
  const session = await auth();
  if (!session?.user) {
    throw new Error("Unauthorized");
  }

  const post = await db.post.findUnique({ where: { id: postId } });

  if (!post || post.authorId !== session.user.id) {
    throw new Error("Not found or unauthorized");
  }

  await db.post.update({
    where: { id: postId },
    data: { published: !post.published },
  });

  revalidatePath("/dashboard/posts");
  revalidatePath("/posts/" + post.slug);
}
```

## Optimistic Updates

```typescript
// src/components/LikeButton.tsx
"use client";

import { useOptimistic, useTransition } from "react";
import { toggleLike } from "@/app/actions/likes";
import { Heart } from "lucide-react";

interface LikeButtonProps {
  postId: string;
  initialLiked: boolean;
  initialCount: number;
}

export function LikeButton({ postId, initialLiked, initialCount }: LikeButtonProps) {
  const [isPending, startTransition] = useTransition();

  const [optimisticState, addOptimistic] = useOptimistic(
    { liked: initialLiked, count: initialCount },
    (state, newLiked: boolean) => ({
      liked: newLiked,
      count: newLiked ? state.count + 1 : state.count - 1,
    })
  );

  const handleClick = () => {
    startTransition(async () => {
      addOptimistic(!optimisticState.liked);
      await toggleLike(postId);
    });
  };

  return (
    <button
      onClick={handleClick}
      disabled={isPending}
      className="flex items-center gap-2"
    >
      <Heart className={optimisticState.liked ? "fill-red-500 text-red-500" : ""} />
      <span>{optimisticState.count}</span>
    </button>
  );
}
```

## Form with useFormState

```typescript
// src/components/ContactForm.tsx
"use client";

import { useFormState, useFormStatus } from "react-dom";
import { submitContact } from "@/app/actions/contact";

function SubmitButton() {
  const { pending } = useFormStatus();
  return (
    <button type="submit" disabled={pending} className="bg-blue-500 text-white px-4 py-2 rounded">
      {pending ? "Sending..." : "Send Message"}
    </button>
  );
}

export function ContactForm() {
  const [state, formAction] = useFormState(submitContact, { message: "", errors: {} });

  return (
    <form action={formAction} className="space-y-4">
      {state.message && <p className={state.errors ? "text-red-500" : "text-green-500"}>{state.message}</p>}

      <div>
        <input name="name" placeholder="Your name" className="w-full border rounded px-3 py-2" />
        {state.errors?.name && <p className="text-red-500 text-sm">{state.errors.name}</p>}
      </div>

      <div>
        <input name="email" type="email" placeholder="Your email" className="w-full border rounded px-3 py-2" />
        {state.errors?.email && <p className="text-red-500 text-sm">{state.errors.email}</p>}
      </div>

      <div>
        <textarea name="message" placeholder="Your message" rows={4} className="w-full border rounded px-3 py-2" />
        {state.errors?.message && <p className="text-red-500 text-sm">{state.errors.message}</p>}
      </div>

      <SubmitButton />
    </form>
  );
}
```

## Best Practices for Google Antigravity IDE

When using Server Actions with Google Antigravity, validate all input with Zod. Use revalidatePath and revalidateTag for cache invalidation. Implement optimistic updates for better UX. Handle errors gracefully. Let Gemini 3 generate server actions from your requirements.

Google Antigravity excels at building full-stack features with Server Actions.

When to Use This Prompt

This Next.js prompt is ideal for developers working on:

  • Next.js applications requiring modern best practices and optimal performance
  • Projects that need production-ready Next.js code with proper error handling
  • Teams looking to standardize their next.js development workflow
  • Developers wanting to learn industry-standard Next.js patterns and techniques

By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their next.js implementations.

How to Use

  1. Copy the prompt - Click the copy button above to copy the entire prompt to your clipboard
  2. Paste into your AI assistant - Use with Claude, ChatGPT, Cursor, or any AI coding tool
  3. Customize as needed - Adjust the prompt based on your specific requirements
  4. Review the output - Always review generated code for security and correctness
💡 Pro Tip: For best results, provide context about your project structure and any specific constraints or preferences you have.

Best Practices

  • ✓ Always review generated code for security vulnerabilities before deploying
  • ✓ Test the Next.js code in a development environment first
  • ✓ Customize the prompt output to match your project's coding standards
  • ✓ Keep your AI assistant's context window in mind for complex requirements
  • ✓ Version control your prompts alongside your code for reproducibility

Frequently Asked Questions

Can I use this Next.js prompt commercially?

Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.

Which AI assistants work best with this prompt?

This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.

How do I customize this prompt for my specific needs?

You can modify the prompt by adding specific requirements, constraints, or preferences. For Next.js projects, consider mentioning your framework version, coding style, and any specific libraries you're using.

Related Prompts

💬 Comments

Loading comments...