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?
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.
# 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.
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).
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.
Currently, you can't access them. There's a discussion in the W3C ServiceWorker repo about adding methods to access them in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With