Looks like Service Worker runs in a worker context and has no access to the DOM. However, once Service Worker installed, I want my users to know that the app will now work offline. How can I do that?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
After testing @Prototype Chain's answer above, I wanted to use
named functions
as opposed to nestedanonymous functions
as event handlers to make code more pleasant to look at for my taste, and hopefully easier to understand later/for others.But only after spending some time sorting docs, I managed to listen correct events on correct objects. So sharing my working example here in hopes of saving someone else from tedious process.
When the Service Worker is in
activated state
, this is the perfect time to display the toast 'Content is cached for offline use
'. Try something below code while registering your service worker.