files/en-us/web/api/cookiestoremanager/subscribe/index.md
{{securecontext_header}}{{APIRef("Cookie Store API")}}{{AvailableInWorkers("window_and_service")}}
The subscribe() method of the {{domxref("CookieStoreManager")}} interface subscribes a {{domxref("ServiceWorkerRegistration")}} to cookie change events.
subscribe(subscriptions)
subscriptions
name
url
A {{jsxref("Promise")}} that resolves with {{jsxref("undefined")}} when the subscription completes.
subscriptions does not match the service worker registration's {{domxref("ServiceWorkerRegistration.scope","scope")}}.In this example, the {{domxref("ServiceWorkerRegistration")}} represented by registration is subscribing to change events on the cookie named "cookie1" with a scope of "/path1".
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.
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}}
{{Compat}}