Skip to content

[RSC Framework Mode] Server Functions Cookie Setting Issue #14389

@femioladipo

Description

@femioladipo

Reproduction

https://stackblitz.com/edit/github-mu2rn18z?file=app%2Froutes%2Fhome.tsx

File Structure:

app/routes/_index/
├── action.ts
└── route.tsx

Files:

// app/routes/_index/action.ts
"use server";

import { data } from "react-router";

export async function updateCountModule() {
  console.log("updateCountModule");
  
  return data("", {
    headers: [
      ['Set-Cookie', 'count=1'],
    ]
  });
}
// app/routes/_index/route.tsx
import { data } from "react-router";
import type { Route } from "./+types/route";
import { updateCountModule } from "./action";

export function loader() {
  return data({
    data: {
      message: "Hello, Message!",
    },
  });
}

export async function updateCountFunction() {
  "use server";
  console.log("updateCountFunction");
  
  return data("", {
    headers: [
      ['Set-Cookie', 'count=2'],
    ]
  });
}

export function ServerComponent({loaderData}: Route.ComponentProps) {
  return (
    <div>
      <p>{loaderData.data.message}</p>
      <button onClick={updateCountModule}>Update Count Module</button>
      <button onClick={updateCountFunction}>Update Count Function</button>
    </div>
  );
}

Steps:

  1. Create the files above with the exact structure
  2. Run the app and click both buttons
  3. Check browser cookies

System Info

System:
    OS: macOS 15.1.1
    CPU: (10) arm64 Apple M2 Pro
    Memory: 76.27 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 23.3.0 - ~/.nvm/versions/node/v23.3.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v23.3.0/bin/yarn
    npm: 10.9.0 - ~/.nvm/versions/node/v23.3.0/bin/npm
    pnpm: 10.11.0 - ~/Library/pnpm/pnpm
  Browsers:
    Safari: 18.1.1
  npmPackages:
    @react-router/dev: 0.0.0-experimental-4b4fa8f64 => 0.0.0-experimental-4b4fa8f64 
    @react-router/fs-routes: 0.0.0-experimental-4b4fa8f64 => 0.0.0-experimental-4b4fa8f64 
    @react-router/serve: 0.0.0-experimental-4b4fa8f64 => 0.0.0-experimental-4b4fa8f64 
    react-router: 0.0.0-experimental-4b4fa8f64 => 0.0.0-experimental-4b4fa8f64

Used Package Manager

pnpm

Expected Behavior

Cookie Setting: Both server functions would Ideally set cookies on the client via the Set-Cookie headers in the data() return value, or another method.

Actual Behavior

Cookie Setting: Neither server function sets cookies on the client, despite the Set-Cookie headers in the return values

Current Limitation: React Router's route action and loader functions support setting cookies and headers through their return values (e.g., return redirect('/path', { headers: { 'Set-Cookie': 'value' } })). However, server functions called from components don't have equivalent capability - their returned data() with headers doesn't actually set cookies or modify response headers. For server functions to be true equivalents to route functions, they need a way to update headers and cookies, enabling component-triggered actions that can both update state and manage session data simultaneously.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions