wsimmonds/claude-nextjs-skills/nextjs-client-cookie-pattern
Claude Code and agent skill by wsimmonds/claude-nextjs-skills
- community
Checking that the SKILL.md file is still in the repository.
Pattern for client components calling server actions to set cookies in Next.js. Covers the two-file pattern of a client component with user interaction (onClick, form submission) that calls a server action to modify cookies. Use when building features like authentication, preferences, or session management where client-side triggers need to set/modify server-side cookies.
Install
npx vskill@latest install wsimmonds/claude-nextjs-skills/nextjs-client-cookie-patternSKILL.md
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()fromnext/headers - Sets the cookie
Complete Implementation
File 1: Client Component
File 2: Server Action
File Structure
TypeScript: NEVER Use any Type
This codebase has @typescript-eslint/no-explicit-any enabled.
Real-World Examples
Example 1: Theme Toggle
Example 2: Accept Cookies Banner
Example 3: Language Preference
Cookie Options
Common Variations
With Form Submission
With Redirect After Setting Cookie
Why This Pattern?
Can't client components set cookies directly? No. Client components run in the browser, and modern browsers restrict cookie manipulation for security. Server actions run on the server where cookie-setting is allowed.
Why not use a Route Handler (API route)? You can! But server actions are simpler and more integrated with the Next.js App Router pattern.
Server actions are preferred because they're:
- More type-safe
- Less boilerplate
- Better integrated with forms
- Easier to test
Reading Cookies
In Server Components:
In Client Components:
Quick Checklist
When you need to set cookies from a button click:
- [ ] Create client component with
'use client' - [ ] Add onClick handler or form submission
- [ ] Create server action file (e.g.,
app/actions.ts) - [ ] Add
'use server'directive - [ ] Import
cookiesfromnext/headers - [ ] Await
cookies()(Next.js 15+) - [ ] Call
cookieStore.set(name, value, options) - [ ] Import server action in client component
- [ ] Call server action from handler
Summary
Client-Server Cookie Pattern:
- ✅ Client component handles user interaction
- ✅ Server action sets the cookie
- ✅ Two files: component + actions
- ✅ Type-safe with proper TypeScript
- ✅ Secure (httpOnly, secure, sameSite options)
This pattern is the recommended way to handle client-triggered cookie operations in Next.js App Router.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingnextjs-client-cookie-pattern/SKILL.md
Popularity
- Repository stars
- 74
- Repository forks
- 9
- 7-day trend score
- 22
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 17 Mar 2026, 7 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/wsimmonds/claude-nextjs-skills/nextjs-client-cookie-pattern)