wsimmonds avatar

Nextjs Client Cookie Pattern

This is the two-file pattern you need whenever a button or form on the client needs to set a cookie in Next.js. Client components can't touch cookies directly, so you split it: one file with 'use client' for the onClick handler, another with 'use server' for the actual cookie-setting logic. The skill walks through the complete setup with real examples like theme toggles, cookie consent banners, and language selectors. It also covers when to use httpOnly versus readable cookies, form submissions, and post-cookie redirects. If you've ever wondered why your client component can't just set a session cookie, this explains the security constraint and gives you the cleanest workaround.

skills.sh installs
325
GitHub stars
110
last updated
Aug 21, 2026

Install

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

Installs into .claude/skills of the current project.

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

Sponsors

Details

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

More from wsimmonds/claude-nextjs-skills

All 9
1syncfusion avatarsyncfusion-angular-image-editorsyncfusion/angular-ui-components-skillsImplement feature-rich image editing in Angular applications using Syncfusion Image Editor. Use this skill whenever user mentions editing images, adding annotat322skills.sh installs
2syncfusion avatarsyncfusion-angular-diagramsyncfusion/angular-ui-components-skillsBuild and configure Syncfusion EJ2 Angular Diagram for flowcharts, org charts, process diagrams, and data-visualization. Trigger when users ask to create nodes/318skills.sh installs
3syncfusion avatarsyncfusion-angular-treegridsyncfusion/angular-ui-components-skillsImplements Syncfusion Angular TreeGrid for hierarchical data with sorting, filtering, editing, exporting, paging, virtual scrolling, and advanced features. Supp318skills.sh installs
4syncfusion avatarsyncfusion-angular-accumulation-chartsyncfusion/angular-ui-components-skillsImplement and customize Syncfusion Angular Accumulation Charts (Pie, Doughnut, Pyramid, Funnel) with data binding, labels, legends, and tooltips. Use this when 315skills.sh installs
5syncfusion avatarsyncfusion-angular-barcodesyncfusion/angular-ui-components-skillsUse Syncfusion EJ2 Angular Barcode/QR components to generate and customize barcodes in Angular. Trigger for Code39/Code128, EAN/UPC, QR Code, Data Matrix, label313skills.sh installs
6tartinerlabs avatartailwindtartinerlabs/skillsUse when writing Tailwind classes, fixing spacing issues, reviewing CSS, or auditing Tailwind patterns. Enforces v4 best practices for grid and responsive.312skills.sh installs