I am setting a cookie "browsing_id" with a uuid v4. I set it in the middleware and I read it in a server action. It is working fine except for the first time the server action is called.
// middleware.ts
let id = req.cookies.get("browsing_id")?.value;
if (id == null) {
id = v4();
req.cookies.set({ name: "browsing_id", value: id });
}
res.cookies.set({ name: "browsing_id", value: id });
In my server action im just using cookes() from next/headers
On the first page load the cookies().get("browsing_id") returns undefined the subsequent reloads the cookies().get("browsing_id") returns the uuid v4 how can i make it work the first time.