nextjs-client-cookie-pattern

Set secure HTTP cookies from Next.js client components via server actions.

Updated Mar 11, 2026
One-click install
npx skills add https://github.com/sckroll/nextjs-mariadb-sample --skill nextjs-client-cookie-pattern-sckroll
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: nextjs-client-cookie-pattern
Source: https://github.com/sckroll/nextjs-mariadb-sample/tree/main/.agents/skills/nextjs-client-cookie-pattern
Command: npx skills add https://github.com/sckroll/nextjs-mariadb-sample --skill nextjs-client-cookie-pattern-sckroll

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill addresses the common challenge in Next.js applications where client-side interactions (like button clicks or form submissions) need to trigger the setting of secure, server-side cookies, which is not directly possible from the browser for security reasons.

Core Features & Use Cases

  • Client-Triggered Cookie Setting: Enables client components to initiate cookie modifications via server actions.
  • Secure Cookie Management: Utilizes Next.js server actions and the cookies() API for secure cookie manipulation (e.g., httpOnly, secure, sameSite options).
  • Use Case: Implementing user preferences (like dark mode), authentication tokens, or session management where a user's action on the client needs to persist state via cookies.

Quick Start

Use the nextjs-client-cookie-pattern skill to create a client component that calls a server action to set a 'user-preference' cookie with the value 'dark-mode'.

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 secure HTTP cookies from a Next.js client component?

To set secure HTTP cookies from a Next.js client component, invoke a Next.js server action that uses the cookies() API to apply secure attributes like httpOnly and sameSite, bridging the client interaction with server-side manipulation.

Why can't I set httpOnly cookies directly in the browser?

You cannot set httpOnly cookies directly in the browser because httpOnly restricts client-side JavaScript access for security. The nextjs-client-cookie-pattern solves this by triggering Next.js server actions to apply these secure cookie attributes safely.

What is the best way to manage user authentication sessions with Next.js server actions?

The best way to manage authentication sessions with Next.js server actions is to pass user actions from client components to server actions, using the cookies() API to securely set authentication tokens with httpOnly and secure attributes.

Does the Next.js cookies() API work with server actions for setting user preferences?

Yes, the Next.js cookies() API works with server actions for setting user preferences. Client components can trigger server actions to securely persist preferences like dark mode by setting server-side cookies.

Can I use Next.js server actions to set cookies for dark mode toggles?

Yes, you can use Next.js server actions to set cookies for dark mode toggles. Client components invoke the server action, which uses the cookies() API to securely persist the dark mode preference as an HTTP cookie.

Are there limitations to setting cookies from Next.js client actions?

The main constraint of setting cookies from Next.js client actions is that client components cannot manipulate cookies directly. You must implement a bridge using server actions and the cookies() API to ensure secure cookie attributes are applied server-side.