From 4e011c555bc13d02ae31650d413c62dc872cc905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?gr=C3=A9goire=20parant?= Date: Wed, 2 Nov 2022 18:46:56 +0100 Subject: [PATCH] HotFix query url for Json manifest (#2761) --- play/public/service-worker-dev.js | 8 ++++++++ play/public/service-worker-prod.js | 8 ++++++++ play/src/front/Network/ServiceWorker.ts | 20 +++++++++++++++++-- .../src/pusher/controllers/FrontController.ts | 5 ++++- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/play/public/service-worker-dev.js b/play/public/service-worker-dev.js index e8b9e7ca57..87ea1ceeb3 100644 --- a/play/public/service-worker-dev.js +++ b/play/public/service-worker-dev.js @@ -4,6 +4,14 @@ let urlsToCache = [ ]; self.addEventListener('install', function(event) { + // url to cache + if(event.target && event.target.serviceWorker && event.target.serviceWorker.scriptURL){ + const url = new URL(event.target.serviceWorker.scriptURL); + const searchParams = new URLSearchParams(url.search); + const playUri = searchParams.get('playUri'); + urlsToCache = [playUri]; + } + // Perform install steps event.waitUntil( caches.open(CACHE_NAME) diff --git a/play/public/service-worker-prod.js b/play/public/service-worker-prod.js index 80486623eb..0a4a3099b7 100644 --- a/play/public/service-worker-prod.js +++ b/play/public/service-worker-prod.js @@ -4,6 +4,14 @@ let urlsToCache = [ ]; self.addEventListener('install', function(event) { + // url to cache + if(event.target && event.target.serviceWorker && event.target.serviceWorker.scriptURL){ + const url = new URL(event.target.serviceWorker.scriptURL); + const searchParams = new URLSearchParams(url.search); + const playUri = searchParams.get('playUri'); + urlsToCache = [playUri]; + } + // Perform install steps event.waitUntil( caches.open(CACHE_NAME) diff --git a/play/src/front/Network/ServiceWorker.ts b/play/src/front/Network/ServiceWorker.ts index bb61c9e964..8e57029b6c 100644 --- a/play/src/front/Network/ServiceWorker.ts +++ b/play/src/front/Network/ServiceWorker.ts @@ -3,6 +3,18 @@ import { NODE_ENV } from "../Enum/EnvironmentVariable"; export class _ServiceWorker { constructor() { if ("serviceWorker" in navigator) { + if (navigator.storage && navigator.storage.persist) { + navigator.storage + .persist() + .then((persistent) => { + if (persistent) { + console.log("Storage will not be cleared except by explicit user action"); + } else { + console.log("Storage may be cleared by the UA under storage pressure."); + } + }) + .catch((err) => console.error("_ServiceWorker => err", err)); + } this.init(); } } @@ -11,7 +23,9 @@ export class _ServiceWorker { //Check node env and if is development, use service worker dev file if (NODE_ENV === "development") { navigator.serviceWorker - .register("/service-worker-dev.js") + .register( + `/service-worker-dev.js?playUri=${window.location.protocol}//${window.location.host}${window.location.pathname}` + ) .then((serviceWorker) => { console.info("Service Worker registered: ", serviceWorker); }) @@ -21,7 +35,9 @@ export class _ServiceWorker { return; } navigator.serviceWorker - .register("/service-worker-prod.js") + .register( + `/service-worker-prod.js?playUri=${window.location.protocol}//${window.location.host}${window.location.pathname}` + ) .then((serviceWorker) => { console.info("Service Worker registered: ", serviceWorker); }) diff --git a/play/src/pusher/controllers/FrontController.ts b/play/src/pusher/controllers/FrontController.ts index 87bde4c0a4..acaa11b345 100644 --- a/play/src/pusher/controllers/FrontController.ts +++ b/play/src/pusher/controllers/FrontController.ts @@ -79,7 +79,10 @@ export class FrontController extends BaseHttpController { }); this.app.get("/static/images/favicons/manifest.json", (req: Request, res: Response) => { - return this.displayManifestJson(req, res, req.get("url")); + if (req.query.url == undefined) { + return res.status(500).send("playUrl is empty in query pramater of the request"); + } + return this.displayManifestJson(req, res, req.query.url.toString()); }); this.app.get("/login", (req: Request, res: Response) => {