name + domain + path as the unique key.new Cookies(cookieStr?: string, isSetCookie?: boolean): Cookies;
Parameter Name | Type | Required | Description |
cookieStr | string | No | |
isSetCookie | boolean | No |
cookies.get(name?: string): null | Cookie | Array<Cookie>;
name matches exist, an array of Cookie objects is returned.Parameter Name | Type | Required | Description |
name | string | No | Cookie name. Values: Default name Indicates obtaining all Cookie objects. Specified name Indicates obtaining the Cookie object with the specified name. If multiple matches exist, a Cookie array is returned. |
Cookie object are as follows. For details, see MDN official documentation Set-Cookie.Property Name | Type | Read-Only | Description |
name | string | Yes | Cookie name. |
value | string | Yes | Cookie value. |
domain | string | Yes | Domain for the Cookie. |
path | string | Yes | Path for the Cookie. |
expires | string | Yes | |
max_age | string | Yes | The Cookie expires after max_age seconds. The unit is seconds (s). |
samesite | string | Yes | Controls protection against Cookie cross-site request forgery (CSRF) attacks. |
httponly | boolean | Yes | Prohibits JavaScript from accessing the Cookie. The Cookie can only be carried in HTTP requests. |
secure | boolean | Yes | The Cookie can only be carried over HTTPS. |
cookies.set(name: string, value: string, options?: Cookie): boolean;
name + domain + path as the unique key.cookies.append(name: string, value: string, options?: Cookie): boolean;
name + domain + path as the unique key.cookies.remove(name: string, options?: Cookie): boolean;
name + domain + path as the unique key. " ( ) , / : ; < = > ? @ [ ] \\ { }, 0x00 to 0x1F, and 0x7F to 0xFF, are automatically escaped. , ; " \\, 0x00 to 0x1F, and 0x7F to 0xFF, are automatically escaped.value, domain, path, expires, max_age, samesite does not exceed 1KB.function handleEvent(event) {const request = event.request;// Parse the Cookie request header (invalid fragments are automatically ignored)const cookies = new Cookies(request.headers.get('Cookie') || '');// Read the login state marker cookie// get may return null / a single Cookie / an array of Cookies (duplicates with the same name)const loginState = cookies.get('login_state');const loggedIn = loginState !== null &&!Array.isArray(loginState) &&loginState.value === '1';if (!loggedIn) {// Not logged in: redirect to the login page with the return URLconst backUrl = encodeURIComponent(request.url);return event.respondWith(Response.redirect(`https://www.example.com/login?back_url=${backUrl}`, 302));}// Logged in: allow origin-pullreturn;}
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback