diff --git a/index.html b/index.html index 2a222ae99..83c7380f6 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,8 @@ + + + diff --git a/pwa-worker.js b/pwa-worker.js new file mode 100644 index 000000000..26143d964 --- /dev/null +++ b/pwa-worker.js @@ -0,0 +1,88 @@ +/* +Copyright 2015, 2019, 2020 Google LLC. All Rights Reserved. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +const CACHE_NAME = 'offline'; + +// Customize this with a different URL if needed. +const OFFLINE_URL = 'static/offline.html'; + +self.addEventListener('install', event => { + event.waitUntil( + (async () => { + const cache = await caches.open(CACHE_NAME); + + + // Setting {cache: 'reload'} in the new request will ensure that the + // response isn't fulfilled from the HTTP cache; i.e., it will be from + // the network. + await cache.add(new Request(OFFLINE_URL, { cache: 'reload' })); + })() + ); + + // Force the waiting service worker to become the active service worker. + self.skipWaiting(); +}); + +self.addEventListener('activate', event => { + event.waitUntil( + (async () => { + // Enable navigation preload if it's supported. + // See https://developers.google.com/web/updates/2017/02/navigation-preload + if ('navigationPreload' in self.registration) { + await self.registration.navigationPreload.enable(); + } + })() + ); + + // Tell the active service worker to take control of the page immediately. + self.clients.claim(); +}); + +self.addEventListener('fetch', event => { + // We only want to call event.respondWith() if this is a navigation request + // for an HTML page. + if (event.request.mode === 'navigate') { + event.respondWith((async () => { + try { + // First, try to use the navigation preload response if it's supported. + const preloadResponse = await event.preloadResponse; + + if (preloadResponse) { + return preloadResponse; + } + + // Always try the network first. + const networkResponse = await fetch(event.request); + + return networkResponse; + } catch (error) { + // catch is only triggered if an exception is thrown, which is likely + // due to a network error. + // If fetch() returns a valid HTTP response with a response code in + // the 4xx or 5xx range, the catch() will NOT be called. + console.log('Fetch failed; returning offline page instead.', error); + + const cache = await caches.open(CACHE_NAME); + const cachedResponse = await cache.match(OFFLINE_URL); + + return cachedResponse; + } + })()); + } + + // If our if() condition is false, then this fetch handler won't intercept the + // request. If there are any other fetch handlers registered, they will get a + // chance to call event.respondWith(). If no fetch handlers call + // event.respondWith(), the request will be handled by the browser as if there + // were no service worker involvement. +}); diff --git a/static/offline.html b/static/offline.html new file mode 100644 index 000000000..0aaf7c67c --- /dev/null +++ b/static/offline.html @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + +
+
+ + + +
+

Connection error

+ Your device may be offline or our servers may be experiencing problems. +
+ + + \ No newline at end of file diff --git a/static/pwa/icons/icon192.png b/static/pwa/icons/icon192.png new file mode 100644 index 000000000..665d1a314 Binary files /dev/null and b/static/pwa/icons/icon192.png differ diff --git a/static/pwa/icons/icon512.png b/static/pwa/icons/icon512.png new file mode 100644 index 000000000..399cb1c58 Binary files /dev/null and b/static/pwa/icons/icon512.png differ diff --git a/static/pwa/manifest.json b/static/pwa/manifest.json new file mode 100644 index 000000000..fb16caf3f --- /dev/null +++ b/static/pwa/manifest.json @@ -0,0 +1,30 @@ +{ + "android_package_name": "org.jitsi.meet", + "prefer_related_applications": true, + "related_applications": [ + { + "id": "org.jitsi.meet", + "platform": "chromeos_play" + } + ], + "short_name": "Jitsi Meet", + "name": "Jitsi Meet", + "icons": [ + { + "src": "icons/icon192.png", + "type": "image/png", + "sizes": "192x192" + }, + { + "src": "icons/icon512.png", + "type": "image/png", + "sizes": "512x512" + } + ], + "start_url": "/", + "background_color": "#2a3a4b", + "display": "standalone", + "scope": "/", + "theme_color": "#2a3a4b" + } + \ No newline at end of file diff --git a/static/pwa/registrator.js b/static/pwa/registrator.js new file mode 100644 index 000000000..3a0137468 --- /dev/null +++ b/static/pwa/registrator.js @@ -0,0 +1,12 @@ +if ('serviceWorker' in navigator) { + window.addEventListener('load', () => { + navigator.serviceWorker + .register('pwa-worker.js') + .then(reg => { + console.log('Service worker registered.', reg); + }) + .catch(err => { + console.log(err); + }); + }); +}