CookieStoreManager: subscribe() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

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

Note: This feature is available in Service Workers.

The subscribe() method of the CookieStoreManager interface subscribes a ServiceWorkerRegistration to cookie change events.

Duplicate subscriptions are ignored: that is, if a service worker subscribes more than once to the same cookie, it will only receive each change notification once.

Syntax

js
subscribe(subscriptions)

Parameters

subscriptions

An array of objects, each of which has the following properties:

name Optional

A string equal to the name of a cookie. If name is omitted, the service worker is subscribed to change events for all cookies that are in scope.

url Optional

A string equal to the URL of a cookie scope. This may be narrower than the scope of the service worker registration. If url is omitted, it defaults to the scope of the service worker registration.

Return value

A Promise that resolves with undefined when the subscription completes.

Exceptions

TypeError

Thrown if the url is not a valid URL, or doesn't start with the service worker registration's scope.

Examples

Setting name and URL

In this example, the ServiceWorkerRegistration represented by registration is subscribing to change events on the cookie named "cookie1" with a scope of "/path1".

js
// Subscribe to a specific cookie and URL
const subscriptions = [{ name: "cookie1", url: `/path1` }];
await registration.cookies.subscribe(subscriptions);

Setting name only

In this example, we set only name and omit url: the subscription applies to all cookies named cookie1 within the service worker's scope.

js
// Subscribe to all cookies named "cookie1" in the registration scope
await registration.cookies.subscribe([{ name: "cookie1" }]);

Setting URL only

In this example we set only url, and omit name: the subscription applies to all cookies within the specified URL scope.

js
// Subscribe to all cookie changes within a specific path
await registration.cookies.subscribe([{ url: "/path/one/" }]);

Subscribing to all cookies

In this example, both name and url are omitted. The subscription applies to all cookies within the service worker's scope.

js
// Subscribe to all cookie changes within the entire registration scope
await registration.cookies.subscribe([{}]);

Setting a URL outside the service worker's scope

If the URL is outside the service worker's scope, subscribe() will throw a TypeError.

js
await registration.cookies.subscribe([
  { name: "cookie1", url: "/out-of-scope/" },
]);

Specifications

Specification
Cookie Store API
# dom-cookiestoremanager-subscribe

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
subscribe
Chrome – Full support
Chrome 87 (Release date: 2020-11-17)
footnote Full support
Edge – Full support
Edge 87 (Release date: 2020-11-19)
footnote Full support
Firefox – Full support
Firefox 140 (Release date: 2025-06-24)
footnote Full support
Opera – Full support
Opera 73 (Release date: 2020-12-09)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 87 (Release date: 2020-11-17)
footnote Full support
Firefox for Android – Full support
Firefox for Android 140 (Release date: 2025-06-24)
footnote Full support
Opera Android – Full support
Opera Android 62 (Release date: 2021-02-16)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 14 (Release date: 2021-04-17)
footnote Full support
WebView Android – Full support
WebView Android 87 (Release date: 2020-11-17)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support