From 707040b506a080b029bf8d92c139377d04628ff0 Mon Sep 17 00:00:00 2001 From: Gregoire Parant Date: Wed, 4 Aug 2021 13:33:58 +0200 Subject: [PATCH] improve PWA and last room strating --- front/dist/resources/service-worker.html | 80 ------------------- front/dist/resources/service-worker.js | 61 -------------- .../dist/static/images/favicons/manifest.json | 4 +- front/src/Connexion/ConnectionManager.ts | 9 +++ front/src/Connexion/LocalUserStore.ts | 15 ++++ front/src/Network/ServiceWorker.ts | 18 ++--- 6 files changed, 34 insertions(+), 153 deletions(-) delete mode 100644 front/dist/resources/service-worker.html delete mode 100644 front/dist/resources/service-worker.js diff --git a/front/dist/resources/service-worker.html b/front/dist/resources/service-worker.html deleted file mode 100644 index 9a1f6379..00000000 --- a/front/dist/resources/service-worker.html +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - WorkAdventure PWA - - - - - WorkAdventure logo -

Charging your workspace ...

-

- - - \ No newline at end of file diff --git a/front/dist/resources/service-worker.js b/front/dist/resources/service-worker.js deleted file mode 100644 index d9509b6f..00000000 --- a/front/dist/resources/service-worker.js +++ /dev/null @@ -1,61 +0,0 @@ -let CACHE_NAME = 'workavdenture-cache-v1'; -let urlsToCache = [ - '/' -]; - -self.addEventListener('install', function(event) { - // Perform install steps - event.waitUntil( - caches.open(CACHE_NAME) - .then(function(cache) { - console.log('Opened cache'); - return cache.addAll(urlsToCache); - }) - ); -}); - -self.addEventListener('fetch', function(event) { - event.respondWith( - caches.match(event.request) - .then(function(response) { - // Cache hit - return response - if (response) { - return response; - } - - return fetch(event.request).then( - function(response) { - // Check if we received a valid response - if(!response || response.status !== 200 || response.type !== 'basic') { - return response; - } - - // IMPORTANT: Clone the response. A response is a stream - // and because we want the browser to consume the response - // as well as the cache consuming the response, we need - // to clone it so we have two streams. - var responseToCache = response.clone(); - - caches.open(CACHE_NAME) - .then(function(cache) { - cache.put(event.request, responseToCache); - }); - - return response; - } - ); - }) - ); -}); - -self.addEventListener('wait', function(event) { - //TODO wait -}); - -self.addEventListener('update', function(event) { - //TODO update -}); - -self.addEventListener('beforeinstallprompt', (e) => { - //TODO change prompt -}); \ No newline at end of file diff --git a/front/dist/static/images/favicons/manifest.json b/front/dist/static/images/favicons/manifest.json index f7b83b60..1cf2a835 100644 --- a/front/dist/static/images/favicons/manifest.json +++ b/front/dist/static/images/favicons/manifest.json @@ -116,12 +116,12 @@ "type": "image\/png" } ], - "start_url": "/resources/service-worker.html", + "start_url": "/", "background_color": "#000000", "display_override": ["window-control-overlay", "minimal-ui"], "display": "standalone", "orientation": "portrait-primary", - "scope": "/resources/", + "scope": "/", "lang": "en", "theme_color": "#000000", "shortcuts": [ diff --git a/front/src/Connexion/ConnectionManager.ts b/front/src/Connexion/ConnectionManager.ts index 11f03a9e..38272737 100644 --- a/front/src/Connexion/ConnectionManager.ts +++ b/front/src/Connexion/ConnectionManager.ts @@ -105,6 +105,15 @@ class ConnectionManager { let roomPath: string; if (connexionType === GameConnexionTypes.empty) { roomPath = window.location.protocol + "//" + window.location.host + START_ROOM_URL; + //get last room path from cache api + try { + const lastRoomUrl = await localUserStore.getLastRoomUrlCacheApi(); + if (lastRoomUrl != undefined) { + roomPath = lastRoomUrl; + } + } catch (err) { + console.error(err); + } } else { roomPath = window.location.protocol + diff --git a/front/src/Connexion/LocalUserStore.ts b/front/src/Connexion/LocalUserStore.ts index 07c2487e..25b673ac 100644 --- a/front/src/Connexion/LocalUserStore.ts +++ b/front/src/Connexion/LocalUserStore.ts @@ -17,6 +17,8 @@ const authToken = "authToken"; const state = "state"; const nonce = "nonce"; +const cacheAPIIndex = "workavdenture-cache-v1"; + class LocalUserStore { saveUser(localUser: LocalUser) { localStorage.setItem("localUser", JSON.stringify(localUser)); @@ -116,10 +118,23 @@ class LocalUserStore { setLastRoomUrl(roomUrl: string): void { localStorage.setItem(lastRoomUrl, roomUrl.toString()); + caches.open(cacheAPIIndex).then((cache) => { + const stringResponse = new Response(JSON.stringify({ roomUrl })); + cache.put(`/${lastRoomUrl}`, stringResponse); + }); } getLastRoomUrl(): string { return localStorage.getItem(lastRoomUrl) ?? ""; } + getLastRoomUrlCacheApi(): Promise { + return caches.open(cacheAPIIndex).then((cache) => { + return cache.match(`/${lastRoomUrl}`).then((res) => { + return res?.json().then((data) => { + return data.roomUrl; + }); + }); + }); + } setAuthToken(value: string | null) { value ? localStorage.setItem(authToken, value) : localStorage.removeItem(authToken); diff --git a/front/src/Network/ServiceWorker.ts b/front/src/Network/ServiceWorker.ts index 9bbcca85..59f913e2 100644 --- a/front/src/Network/ServiceWorker.ts +++ b/front/src/Network/ServiceWorker.ts @@ -6,15 +6,13 @@ export class _ServiceWorker { } init() { - window.addEventListener("load", () => { - navigator.serviceWorker - .register("/resources/service-worker.js") - .then((serviceWorker) => { - console.info("Service Worker registered: ", serviceWorker); - }) - .catch((error) => { - console.error("Error registering the Service Worker: ", error); - }); - }); + navigator.serviceWorker + .register("/service-worker.js") + .then((serviceWorker) => { + console.info("Service Worker registered: ", serviceWorker); + }) + .catch((error) => { + console.error("Error registering the Service Worker: ", error); + }); } }