Cookie Store API

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The Cookie Store API provides an asychronous API for managing cookies, while also exposing cookies to service workers.

Concepts and Usage

The existing method of getting and setting cookies involves working with document.cookie as a string of key/value pairs. In addition to this being cumbersome and error prone, it also has a host of issues in the context of modern web development.

The document.cookie interface is synchronous, single-threaded, and blocking. When writing a cookie you must wait for the browser to update the string of all cookies. In addition, the reliance on document means that cookies cannot be accessed by service workers which cannot access the document object.

The Cookie Store API provides an updated method of managing cookies. It is asynchronous and promise-based, therefore does not block the event loop. It does not rely on document and so is available to service workers. The methods for getting and setting cookies also provide more feedback by way of error messages. This means that web developers do not have to set then immediately read back a cookie to check that setting was successful.

Interfaces

CookieStore

The CookieStore interface enables getting and setting cookies.

CookieStoreManager

The CookieStoreManager interface provides a service worker registration to enable service workers to subscribe to cookie change events.

Events

CookieChangeEvent

A CookieChangeEvent is dispatched against CookieStore objects in Window contexts when any script-visible cookies changes occur.

ExtendableCookieChangeEvent

A ExtendableCookieChangeEvent is dispatched against ServiceWorkerGlobalScope events when any script-visible cookie changes have occurred which match the service worker's cookie change subscription list.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
Cookie_Store_API
87
87
No
No
73
No
87
87
No
62
No
14.0
delete
87
87
No
No
73
No
87
87
No
62
No
14.0
get
87
87
No
No
73
No
87
87
No
62
No
14.0
getAll
87
87
No
No
73
No
87
87
No
62
No
14.0
onchange
87
87
No
No
73
No
87
87
No
62
No
14.0
set
87
87
No
No
73
No
87
87
No
62
No
14.0

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Cookie_Store_API