I am using type script for my app and i need to store some id's on service worker, but it is not supporting self.localStorage.setItem('notificationclicked', 'bgclicked')
Asked
Active
Viewed 2,588 times
0

pushkin
- 9,575
- 15
- 51
- 95

abhishek gupta
- 1
- 1
- 1
-
1You can't access local storage from service worker – Aleksey L. Sep 17 '18 at 13:08
-
Possible duplicate of [Access localStorage from service worker](https://stackoverflow.com/questions/40887635/access-localstorage-from-service-worker) – Aleksey L. Sep 18 '18 at 05:04
-
Hi Aleksey , do you know any work around for this? Actually i need to handle background notification click on my PWA app to display a different page. – abhishek gupta Sep 18 '18 at 07:21
-
You can use [postMessage](https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/postMessage) – Aleksey L. Sep 18 '18 at 11:20
1 Answers
3
Synchronous APIs like localStorage, XHR as well as the DOM are not accessible in the service worker.
For storage you will need to use IndexedDB. It is not the easiest API to use directly. Instead I recommend a library that can make it as easy to use as localStorage.
I like localForage, https://localforage.github.io/localForage/

Chris Love
- 3,740
- 1
- 19
- 16
-
Hi chris ,thank you for your reply but the localForge not working on service worker. Have you used it on the service worker? – abhishek gupta Sep 18 '18 at 07:21
-