Cookies can only be modified in a Server Action or Route Handler.
Answered
Japanese flying squid posted this in #help-forum
Japanese flying squidOP
Wheres the problem i dont get it its a server action
⨯ Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options
at getCart (file:...)
at async Home (file:...)
19 | const id = crypto.randomUUID();
20 |
> 21 | cookiesInstance.set("cart", id, {
| ^
22 | httpOnly: true,
23 | secure: true,
24 | sameSite: "strict", {
digest: '1331757352'
}
"use server";
import { client } from "@/sanity/lib/client";
import {
CART_BY_SESSIONID_QUERY,
PRODUCT_BY_ID_QUERY,
} from "@/sanity/lib/queries";
import { cookies } from "next/headers";
import { Cart } from "../../../sanity.types";
export async function getCart(): Promise<Cart> {
let cart: Cart;
const cookiesInstance = await cookies();
const cartCookie = cookiesInstance.get("cart");
if (!cartCookie) {
const id = crypto.randomUUID();
cookiesInstance.set("cart", id, {
httpOnly: true,
secure: true,
sameSite: "strict",
path: "/",
});
cart = await client.create({
_type: "cart",
sessionId: id,
items: [],
price: 0,
});
} else {
const existingCart = await client.fetch(
CART_BY_SESSIONID_QUERY,
{ sessionId: cartCookie.value },
{ useCdn: false }
);
if (!existingCart) {
cookiesInstance.delete("cart");
return getCart();
}
cart = existingCart;
}
return cart;
}
Answered by B33fb0n3
you are using a server action, but it also must be called from a client component. Else it will be just a normal function and not a server action anymore, so just dont modify the cookies inside your page.tsx or layout. Do it inside your middleware for example
--- Edit
More information about it here: https://nextjs-forum.com/post/1337838339918925926#message-1338468640722391084
--- Edit
More information about it here: https://nextjs-forum.com/post/1337838339918925926#message-1338468640722391084
11 Replies
@Japanese flying squid Wheres the problem i dont get it its a server action
ts
⨯ Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options
at getCart (file:...)
at async Home (file:...)
19 | const id = crypto.randomUUID();
20 |
> 21 | cookiesInstance.set("cart", id, {
| ^
22 | httpOnly: true,
23 | secure: true,
24 | sameSite: "strict", {
digest: '1331757352'
}
ts
"use server";
import { client } from "@/sanity/lib/client";
import {
CART_BY_SESSIONID_QUERY,
PRODUCT_BY_ID_QUERY,
} from "@/sanity/lib/queries";
import { cookies } from "next/headers";
import { Cart } from "../../../sanity.types";
export async function getCart(): Promise<Cart> {
let cart: Cart;
const cookiesInstance = await cookies();
const cartCookie = cookiesInstance.get("cart");
if (!cartCookie) {
const id = crypto.randomUUID();
cookiesInstance.set("cart", id, {
httpOnly: true,
secure: true,
sameSite: "strict",
path: "/",
});
cart = await client.create({
_type: "cart",
sessionId: id,
items: [],
price: 0,
});
} else {
const existingCart = await client.fetch(
CART_BY_SESSIONID_QUERY,
{ sessionId: cartCookie.value },
{ useCdn: false }
);
if (!existingCart) {
cookiesInstance.delete("cart");
return getCart();
}
cart = existingCart;
}
return cart;
}
you are using a server action, but it also must be called from a client component. Else it will be just a normal function and not a server action anymore, so just dont modify the cookies inside your page.tsx or layout. Do it inside your middleware for example
--- Edit
More information about it here: https://nextjs-forum.com/post/1337838339918925926#message-1338468640722391084
--- Edit
More information about it here: https://nextjs-forum.com/post/1337838339918925926#message-1338468640722391084
Answer
@B33fb0n3 you are using a server action, but it also must be called from a client component. Else it will be just a normal function and not a server action anymore, so just dont modify the cookies inside your page.tsx or layout. Do it inside your middleware for example
--- Edit
More information about it here: https://discord.com/channels/752553802359505017/1337838339918925926/1338468640722391084
Japanese flying squidOP
Ohh i see so calling it like this is false i should rather call this in a useEffect and make the component to a client component for it to work right?
@Japanese flying squid Ohh i see so calling it like this is false i should rather call this in a useEffect and make the component to a client component for it to work right?
what do you want to do with your getCard function? It sounds like that you only receive data and not set any data (no set of cookies)
@B33fb0n3 what do you want to do with your getCard function? It sounds like that you only receive data and not set any data (no set of cookies)
Japanese flying squidOP
I have pasted the code above
@Japanese flying squid I have pasted the code above
Ah my bad. Yea, setting is now allowed inside the function. Create the cart, when the user adds an item to his cart or use an useEffect. Else (if it's possible) the sanity can set it while creating/adding an item
@Japanese flying squid solved?
@B33fb0n3 <@1194175273134985347> solved?
Japanese flying squidOP
Yeah its solved by calling the server action in a useEffect like you said. According to the Nextjs docs, you cant modify cookies directly from the server because its part of the browser API. You need to do it in a Client Component since that's where the connection to the browser exists.
https://nextjs.org/docs/app/api-reference/functions/cookies#understanding-cookie-behavior-in-server-components
https://nextjs.org/docs/app/api-reference/functions/cookies#understanding-cookie-behavior-in-server-components
thank you
happy to help