service-worker

Passing state info into a service worker before `install`

家住魔仙堡 提交于 2019-11-30 12:04:13
问题 Background I'm new to service workers but working on a library that is intended to become "offline-first" (really, almost "offline-only") (FWIW, the intent is to allow consumers of the library to provide JSON config representing tabular multilinear texts and get in return an app which allows their users to browse these texts in a highly customizable manner by paragraph/verse ranges.) Other projects are to install the library as a dependency and then supply information via our JavaScript API

URL of page currently being served by Service Worker 'fetch' event

可紊 提交于 2019-11-30 09:31:01
问题 How can I get the full URL of the page which is being serviced by a service worker's 'fetch' event? The "self.location" property seems to only refer to the root URL of the site. For example, if page https://example.com/folder/pagename.html is performing a fetch which the service worker is intercepting, the service worker's 'self.location' property returns "https://example.com". event.currentTarget.location and event.explicitOriginalTarget.location, event.originalTarget, and event.target all

What is the use of `self.Clients.claim()`

旧街凉风 提交于 2019-11-30 08:50:11
To register a service worker, I can call navigator.serviceWorker.register('/worker.js') Every time the page loads it checks for an updated version of worker.js . If an update is found, the new worker won't be used until all the page's tabs are closed and then re-opened. The solution I read was: self.addEventListener('install', function(event) { event.waitUntil(self.skipWaiting()); }); self.addEventListener('activate', function(event) { event.waitUntil(self.clients.claim()); }); I can understand the skipWaiting part, but what exactly does clients.claim() do? I've done some simple tests and it

Google Chrome Push Notifications not working if the browser is closed?

牧云@^-^@ 提交于 2019-11-30 08:23:55
It is written here that the push notifications will work even if the browser is closed, but I tested it and it is not the case. I receive push-notifications only if the browser is open (doesnt matter if the particular webpage is open or not). I tested this on chrome for Desktop & chrome for Android (after updating to latest version). my question is :- For push notifications to work should the browser be open? Note:- I used this for testing. According to Can I Use , Chrome and Firefox desktop browsers require the browser to be running for receiving push notifications; mobile browsers typically

How do I call a method on my ServiceWorker from within my page?

北战南征 提交于 2019-11-30 06:56:04
I have a ServiceWorker registered on my page and want to pass some data to it so it can be stored in an IndexedDB and used later for network requests (it's an access token). Is the correct thing just to use network requests and catch them on the SW side using fetch, or is there something more clever? Note for future readers wondering similar things to me: Setting properties on the SW registration object, e.g. setting self.registration.foo to a function within the service worker and doing the following in the page: navigator.serviceWorker.getRegistration().then(function(reg) { reg.foo; })

Can't find serviceWorker in navigator anymore

↘锁芯ラ 提交于 2019-11-30 06:52:13
问题 since the new update of Google Chrome (version 69.0.3497.92 (official build) (64-bit)) I can't find the serviceWorker service in the Navigator anymore. Actually I could register my Service Worker as follows but now I get an error that serviceWorker cannot be found in the navigator: if('serviceWorker' in navigator) { /* * * Register the Service Worker * * */ navigator.serviceWorker.register('sw.js').then(function(registration) { console.log('Service Worker Registered'); }); } else console.log(

Best practices for detecting offline state in a service worker

六眼飞鱼酱① 提交于 2019-11-30 06:47:16
问题 I have a service worker that is supposed to cache an offline.html page that is displayed if the client has no network connection. However, it sometimes believes the navigator is offline even when it is not. That is, navigator.onLine === false . This means the user may get offline.html instead of the actual content even when online, which is obviously something I'd like to avoid. This is how I register the service worker in my main.js : // Install service worker for offline use and caching if

navigator.serviceWorker is never ready

喜你入骨 提交于 2019-11-30 05:38:05
I registered a service worker successfully, but then the code navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { // Do we already have a push message subscription? .... hangs -- the function is never called. Why? The problem was that the service-worker.js file was stored in an assets sub-directory. Don't do that: store the service-worker.js in the root of your app (or higher). That way your app can access the service-worker. See HTML5Rocks article -- One subtlety with the register method is the location of the service worker file. You'll notice in this case that the

Handling File Uploads When Offline With Service Worker

不羁岁月 提交于 2019-11-30 04:57:55
问题 We have a web app (built using AngularJS) that we're gradually adding PWA 'features' too (service worker, launchable, notifications, etc). One of the features our web app has is the ability to complete a web form while offline. At the moment, we store the data in IndexedDB when offline, and simply encourage the user to push that data to the server once they're online ("This form is saved to your device. Now you're back online, you should save it to the cloud..."). We will do this

How can Chrome extension background script communicate with web page service worker?

蹲街弑〆低调 提交于 2019-11-30 04:56:40
问题 Our webapp registers a service worker. We also have a Chrome extension. What we need is to send messages from service worker to the extension without having the webapp open. What is the right approach to do that? Our current solution is requesting some URL in the service worker and capturing it in the extension's background script using webRequest - chrome.webRequest.onBeforeRequest.addListener . It works but it looks pretty much nonstandardly. 回答1: (from wOxxOm's comment, since he hates