nextjs-client-cookie-pattern

Set server-side cookies from Next.js client components via server actions.

Updated Apr 23, 2025
One-click install
npx skills add https://github.com/Cyberworld-builders/legend --skill nextjs-client-cookie-pattern-cyberworld-builders
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: nextjs-client-cookie-pattern
Source: https://github.com/Cyberworld-builders/legend/tree/main/.claude/skills/nextjs-client-cookie-pattern
Command: npx skills add https://github.com/Cyberworld-builders/legend --skill nextjs-client-cookie-pattern-cyberworld-builders

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill addresses the challenge of setting server-side cookies initiated by client-side user interactions in Next.js applications, ensuring secure and efficient state management.

Core Features & Use Cases

  • Client-Triggered Cookie Setting: Enables client components to invoke server actions for modifying cookies.
  • Secure Cookie Management: Utilizes httpOnly, secure, and sameSite options for robust security.
  • Use Case: Implementing user preferences like dark mode, managing authentication tokens, or handling session data where a user action (e.g., clicking a button) needs to persist state via cookies.

Quick Start

Use the nextjs-client-cookie-pattern skill to enable dark mode by calling the setPreference server action from a client component.

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

Next.js client components can set server-side cookies by invoking server actions that use next/headers for cookie manipulation. This pattern securely handles client-triggered state changes, ensuring sensitive logic remains server-side.

What is the best way to manage authentication tokens in Next.js server actions?

Managing authentication tokens in Next.js server actions involves using next/headers to apply httpOnly, secure, and sameSite attributes. This ensures robust security for session data triggered by client-side actions.

Can I use server actions to save user preferences like dark mode in Next.js?

Yes, you can use Next.js server actions to save user preferences like dark mode. A client component triggers an action, such as setPreference, which then securely writes the preference to a server-side cookie.

Does the next/headers API support secure cookie attributes for session management?

The next/headers API supports secure cookie configuration for session management by allowing you to set httpOnly, secure, and sameSite attributes. This ensures robust security when modifying cookies via server actions.

Why should I use server actions for cookie manipulation instead of client-side JavaScript?

Using server actions for cookie manipulation instead of client-side JavaScript prevents exposing sensitive cookie logic to the browser. This Next.js pattern enforces secure attributes like httpOnly, protecting authentication tokens and session data.