W3cubDocs

/Web APIs

CookieStoreManager: subscribe() method

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

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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

Syntax

js

subscribe(subscriptions)

Parameters

subscriptions

An object containing:

name

A string with the name of a cookie.

url

A string with the url of a cookie scope. This may be narrower than 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 passed in subscriptions does not match the service worker registration's scope.

Examples

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

js

const subscriptions = [{ name: "cookie1", url: `/path1` }];
await registration.cookies.subscribe(subscriptions);

The URL passed to the subscribe() method, may be narrower than the service worker registration scope. In the following example the subscription is for /path/one/, so it will receive change events for changes on the first cookie, but not the second.

js

registration.cookies.subscribe([{ name: "cookie1", url: "/path/one/" }]); // subscription
cookieStore.set({ name: "cookie1", value: "cookie-value", path: "/path/one/" }); // receives a change event
cookieStore.set({ name: "cookie1", value: "cookie-value", path: "/path/two/" }); // does not receive a change event

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
subscribe 87 87 No No 73 No 87 87 No 62 No 14.0

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