Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read cookies from within a Service Worker?

I would like to read a cookie within my service worker to use it when setting up my caches, but can't find any way of doing it.

Is there any way of doing that, or will I need to duplicate cookie data into IDB or similar?

like image 808
owencm Avatar asked Feb 17 '16 03:02

owencm


People also ask

Can service workers access IndexedDB?

The Cache Storage API and IndexedDB provide asynchronous access to persistent storage for PWAs and can be accessed from the main thread, web workers, and service workers.

Can a Service worker access cookies?

# Welcome service workersThe Cookie Store API is asynchronous, and therefore is allowed in service workers. Interacting with the cookies works the same way in document contexts and in service workers.

Can service workers access cache?

Using a Service worker you can easily set an app up to use cached assets first, thus providing a default experience even when offline, before then getting more data from the network (commonly known as Offline First).

Can service workers access local storage?

localStorage is synchronous, which is not a tremendous problem for every app, but could lead to issues if you're building something that has significant performance needs. Window. localStorage cannot be used in the context of a Worker or a ServiceWorker because the APIs are not available there.


1 Answers

Currently, you can't access them. There's a discussion in the W3C ServiceWorker repo about adding methods to access them in the future.

like image 89
Marco Castelluccio Avatar answered Nov 11 '22 07:11

Marco Castelluccio