createCookie
Creates a logical container for managing a browser cookie from the server. A Cookie is a container for cookie metadata (name and options) with methods to parse and serialize cookie values.Signature
The name of the cookie as it appears in the
Cookie and Set-Cookie headers.Configuration options for the cookie.
Array of secrets used to sign/unsign cookie values. The first secret is used for signing new cookies. All secrets are tried when parsing to support secret rotation.
Specifies the domain for the cookie. Defaults to the current domain.
Specifies the URL path that must exist in the requested URL for the browser to send the cookie.
Maximum age of the cookie in seconds. Takes precedence over
expires.Expiration date of the cookie. Use
maxAge instead for relative expiration.When
true, the cookie is inaccessible to JavaScript’s document.cookie API.When
true, the cookie is only sent over HTTPS connections.Controls when the cookie is sent with cross-site requests.
"strict"- Cookie is only sent for same-site requests"lax"- Cookie is sent for top-level navigations and same-site requests"none"- Cookie is sent for all requests (requiressecure: true)
Returns
A cookie container object with the following properties and methods:
The name of the cookie.
true if the cookie uses one or more secrets for signing.The expiration date of the cookie, calculated from
maxAge or expires option.Parses a raw
Cookie header and returns the value of this cookie or null if not present.Serializes a value and returns the
Set-Cookie header string.Basic Example
Parse Cookie Values
Extract cookie values from incoming requests:filename=app/routes/theme.tsx
Serialize Cookie Values
Set cookies in responses:filename=app/routes/set-theme.tsx
Signed Cookies
Sign cookies to prevent tampering:Secret Rotation
Rotate secrets without invalidating existing cookies:Override Options at Runtime
You can override cookie options when serializing:Delete a Cookie
Set a cookie with past expiration:Complex Data Types
Cookies automatically serialize and deserialize JSON:Security Considerations
Always Sign Sensitive Cookies
Prevent tampering by signing cookies that contain important data:Use HttpOnly for Session Cookies
Prevent XSS attacks from accessing sensitive cookies:Secure in Production
Always enablesecure in production to prevent man-in-the-middle attacks:
Cookie Size Limits
Browsers limit cookie size to about 4KB. For larger data, use session storage instead:Related
- createCookieSessionStorage - Store session data in cookies
- createSessionStorage - Custom session storage
- Sessions and Cookies - Sessions guide