setCookie not working in latest version
Which project does this relate to?
Router
Describe the bug
setCookie doesn't work. It doesn't add a cookie.
Your Example Website or App
https://codesandbox.io/p/devbox/amazing-banach-y575zd?workspaceId=ws_RSj8qoo2J6ibhjuWAEjYJP
Steps to Reproduce the Bug or Issue
import { createFileRoute } from "@tanstack/react-router";
import { setCookie, getCookie } from "@tanstack/start/server";
import { createServerFn } from "@tanstack/start";
const cookieSettingServerFn = createServerFn({ method: "POST" }).handler(
async () => {
setCookie("this-cookie-doesnt-work", "value");
}
);
export const Route = createFileRoute("/")({
component: Home,
loader: async () => {
setCookie("this-cookie-works", "value"); // doesn't work
await cookieSettingServerFn(); // doesnt work
console.log(getCookie("this-cookie-works")); // undefined
console.log(getCookie("this-cookie-doesnt-work")); // undefined
},
});
function Home() {
return (
<div className="p-2">
<h3>Welcome Home!!!</h3>
</div>
);
}
Expected behavior
cookies should be added
Screenshots or Videos
No response
Platform
chrome, mac, no extensions
Additional context
Notably this partially works for me on my local repo
loader: async () => {
setCookie("this-cookie-works", "value"); // works
await cookieSettingServerFn(); // doesnt work
console.log(getCookie("this-cookie-works")); // value
console.log(getCookie("this-cookie-doesnt-work")); // undefined
},
1.102.1
Looks like it hasn't been working properly for some time. I downgraded to 1.98.6 and the following is not working
import { createFileRoute } from "@tanstack/react-router";
import { createServerFn } from "@tanstack/start";
import { setCookie } from "@tanstack/start/server";
export const fetchUser = createServerFn({ method: "POST" }).handler(
async () => {
setCookie("name", "value");
return { userId: "123" };
}
);
export const Route = createFileRoute("/_authed/user")({
beforeLoad: async () => {
await fetchUser(); // this never sets a cookie
},
component: RouteComponent,
});
function RouteComponent() {
return <div>Hello "/_authed/user"!</div>;
}
I noticed that settingCookie inside createServerFn works if you call it from client side but doesn't works if you call it inside loader function.
import { createFileRoute } from '@tanstack/react-router';
import { createServerFn } from '@tanstack/start';
import { getCookie, setCookie } from '@tanstack/start/server';
const cookieSettingServerFn = createServerFn({ method: 'POST' }).handler(
async () => {
setCookie('button-will-set-cookie', 'value');
}
);
export const Route = createFileRoute('/')({
component: Home,
loader: async () => {
setCookie('this-cookie-works', 'value'); // Running 1st Time so value is undefined but then it will be value
await cookieSettingServerFn(); // Doesn't work -> This will not set the cookie
// Maybe because a server fn is called inside the loader which runs on server so it's headers are lost maybe?
// Is this how it's supposed to be?
console.log('this-will-work', getCookie('this-cookie-works')); // 1st Time -> undefined, 2nd Time -> value
console.log('button-will-set-cookie', getCookie('button-will-set-cookie')); // This will be undefined till the button is clicked
},
});
function Home() {
const handleClick = async () => {
await cookieSettingServerFn(); // This works -> This will set the cookie
};
return (
<div className="p-2">
<h3>This works!!!</h3>
<button onClick={handleClick}>Set Cookie</button>
</div>
);
}
I have a server function that sets a cookie and runs in beforeLoad. The server function is supposed to set a cookie. However, if you manually edit the URL and navigate to the page, the cookie doesn’t get set. But if you navigate to the page using client-side navigation, the cookie is set correctly.
Reproduction URL https://codesandbox.io/p/devbox/youthful-curran-xl86qh
Check the index.tsx file—I have a server function that sets a cookie. To reproduce the issue:
- Open the browser and remove any cookie named toast if it exists.
- Reload the page. The expected behavior is that the cookie should be set, but it isn’t.
- Now, navigate away from the page and return to it using client-side navigation. You’ll notice that the cookie gets set.
- Now, navigate away from the page and return to it using client-side navigation. You’ll notice that the cookie gets set.
I noticed that you need not to return back to home page. Just hovering over the home link sets the cookie. Even if you are at home page and hover on home link cookie is set.