Cookie Store API
Cookie Store APIwindowandservice
Cookie Store APIwindow_and_service
The **Cookie Store API** is an asynchronous API for managing cookies, available in windows and also [service workers](/docs/Web/API/Service_Worker_API).
## Concepts and Usage
The original method of getting and setting cookies involves working with document.cookie to get and set cookie information 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, as they 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.
- CookieChangeEvent
- : A `CookieChangeEvent` named `change` is dispatched against `CookieStore` objects in Window contexts when any script-visible cookies changes occur.
- ExtendableCookieChangeEvent
- : An `ExtendableCookieChangeEvent` named `cookiechange` is dispatched in ServiceWorkerGlobalScope contexts when any script-visible cookie changes occur that match the service worker's cookie change subscription list.
### Extensions to other interfaces
- ServiceWorkerGlobalScope.cookieStore
- : Returns a reference to the CookieStore object associated with the service worker.
- ServiceWorkerRegistration.cookies
- : Returns a reference to the CookieStoreManager interface, which enables a web app to subscribe to and unsubscribe from cookie change events.
- Window.cookieStore
- : Returns a reference to the CookieStore object for the current document context.
- cookiechange event
- : Fired when any cookie changes have occurred which match the service worker's cookie change subscription list.
## Specifications
## Browser compatibility
## See also
- [Service Worker API](/docs/Web/API/Service_Worker_API)
Cite this
"Cookie Store API". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/Cookie_Store_API JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy