nextjs-client-cookie-pattern

Trigger Next.js server actions from client components to set cookies.

109|21|Updated Oct 23, 2025
One-click install
npx skills add https://github.com/wsimmonds/claude-nextjs-skills --skill nextjs-client-cookie-pattern-wsimmonds
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: nextjs-client-cookie-pattern
Source: https://github.com/wsimmonds/claude-nextjs-skills/tree/main/nextjs-client-cookie-pattern
Command: npx skills add https://github.com/wsimmonds/claude-nextjs-skills --skill nextjs-client-cookie-pattern-wsimmonds

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Client components cannot set cookies directly. Use a two-file pattern where a client component triggers a server action that sets cookies securely.

Core Features & Use Cases

  • Two-file pattern: A client component (with 'use client') calls a server action to modify cookies.
  • Secure cookie handling: Server action uses next/headers cookies to set httpOnly/secure cookies.
  • Real-world examples: Theme toggle that persists, cookie consent banners, language preferences.

Quick Start

Implement a client button that calls a server action to set a 'theme' cookie when clicked.

Frequently Asked Questions about nextjs-client-cookie-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set cookies from client components in Next.js?

Client components cannot set cookies directly. Use a two-file pattern: a client component with 'use client' calls a server action with 'use server' that uses next/headers cookies() to set httpOnly, secure cookies. This pattern enables client-triggered events like button clicks to persist authentication, preferences, or session data server-side.

Can I trigger server actions from client components to modify cookies?

Yes. Client components call server actions that handle cookie modification. The server action receives the cookie key and value, applies strict TypeScript typing, and sets cookie options (httpOnly, secure, sameSite, maxAge) via next/headers cookies(). This separates client interaction from secure server-side state management.

What's the best way to persist user preferences like theme or language in Next.js?

Use the two-file pattern to set persistent cookies from client events. A client component triggers a server action when the user selects a preference, the server action sets a typed, secure cookie with appropriate options, and the preference persists across sessions without exposing cookie logic to the client.

Do I need specific TypeScript typing for Next.js cookie handling?

Yes. Strict TypeScript typings are required for this pattern: define explicit key and value types for cookies, and specify well-defined cookie options (httpOnly, secure, sameSite, maxAge). This ensures type safety when the server action receives client-side inputs and sets cookies via next/headers.

How does the two-file pattern secure cookies in Next.js applications?

The pattern separates concerns: the client component ('use client') handles user interaction, while the server action ('use server') exclusively manages cookie operations with next/headers cookies(). This prevents client-side cookie manipulation, enforces httpOnly and secure flags, and centralizes authentication and session logic server-side.

What real-world use cases require setting cookies from client actions?

Common use cases include cookie consent banners that set preference cookies, theme toggles persisting user design choices, language selectors storing locale preferences, and authentication flows where client events trigger server-side session cookie updates for security and persistence.