Skip to content
Workers
Visit Workers on GitHub
Set theme to dark (⇧+D)

Cookie parsing

Given the cookie name, get the value of a cookie. You can also use cookies for A/B testing.

import { parse } from "worktop/cookie";
// OR
// import { parse } from "cookie"
// The name of the cookie
const COOKIE_NAME = "__uid";
export default {
fetch(req) {
const cookie = parse(req.headers.get("Cookie") || "");
if (cookie[COOKIE_NAME] != null) {
// Respond with the cookie value
return new Response(cookie[COOKIE_NAME]);
}
return new Response("No cookie with name: " + COOKIE_NAME);
},
};