Skip to content
wsimmonds avatar

Nextjs Client Cookie Pattern

Pattern for client components calling server actions that set Next.js cookies on click or submit.

CCM score
43 of 100
skills.sh installs
328
GitHub stars
116
last updated
Aug 21, 2026
View on GitHub

Own this skill? Claim it

What it does

  • Covers the two-file pattern: a 'use client' component with an onClick handler or form action, and a 'use server' action that sets the cookie
  • Includes complete examples for a theme toggle, a cookies-accepted banner, a language selector, and a form-based preferences save
  • Shows cookie options such as httpOnly, secure, sameSite, maxAge, and path, plus a login example that redirects after setting a session cookie
  • Designed for authentication, preference, and session features where a client-side trigger must modify server-side cookies

Install

$ npx -y skills add wsimmonds/claude-nextjs-skills --skill nextjs-client-cookie-pattern -y

Installs into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.

Files

Next.js: Client Component + Server Action Cookie Pattern

Pattern Overview

This pattern handles a common Next.js requirement: client-side interaction (button click) that needs to set server-side cookies.

Why Two Files?

  • Client components ('use client') can have onClick handlers
  • Only server code can set cookies (security requirement)
  • Solution: Client component calls a server action that sets cookies

The Pattern

Scenario: A button that sets a cookie when clicked

File 1: Client Component (app/CookieButton.tsx)

  • Has 'use client' directive
  • Has onClick handler
  • Imports and calls server action

File 2: Server Action (app/actions.ts)

  • Has 'use server' directive
  • Uses cookies() from next/headers
  • Sets the cookie

Complete Implementation

File 1: Client Component

// app/CookieButton.tsx
'use client';

import { setPreference } from './actions';

export default function CookieButton() {
  const handleClick = async () => {
    await setPreference('dark-mode', 'true');
  };

  return (
    <button onClick={handleClick}>
      Enable Dark Mode
    </button>
  );
}

File 2: Server Action

Details

Repository
github.com/wsimmonds/claude-nextjs-skills
Categories
Frontend & UI Code, Backend & APIs
First seen
Jun 3, 2026

Questions

What does the nextjs-client-cookie-pattern skill do?

The nextjs-client-cookie-pattern skill teaches a two-file pattern where a client component with onClick or form submission calls a server action that sets cookies in Next.js. It covers the reason only server code can set cookies, plus examples such as theme toggles, cookie banners and language preferences.

How do I install the nextjs-client-cookie-pattern skill?

Run npx -y skills add wsimmonds/claude-nextjs-skills --skill nextjs-client-cookie-pattern -y in a project. The nextjs-client-cookie-pattern skill then lets the agent build client components that call server actions to set or modify cookies.

Where does nextjs-client-cookie-pattern stand in the Frontend & UI Code category?

nextjs-client-cookie-pattern is in the Frontend & UI Code category, next to vercel-react-best-practices, web-design-guidelines, design-taste-frontend, vercel-composition-patterns and lark-apps. Its focus is narrower, covering only how client interactions set server-side cookies in Next.js.

When should I use the nextjs-client-cookie-pattern skill?

Use nextjs-client-cookie-pattern when building authentication, preferences or session management where a client-side trigger must set or modify server-side cookies. The skill also covers cookie options such as httpOnly, secure, sameSite and maxAge.

More from wsimmonds/claude-nextjs-skills

All 9

Similar to Nextjs Client Cookie Pattern

More Frontend & UI Code