From b005193dbc387c63e0a608772bc470adf44f6281 Mon Sep 17 00:00:00 2001 From: "Randy.Fischer" Date: Wed, 15 Apr 2026 15:54:06 +0200 Subject: [PATCH] Request persistent storage after first save MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IndexedDB data can be evicted under storage pressure, especially on iOS Safari. navigator.storage.persist() asks the browser to keep our data. On Chromium+Firefox it succeeds silently when conditions allow (installed PWA, engagement heuristics); on Safari it typically grants only for installed PWAs. - persistentStorage.js: isPersisted() and requestPersistence() with graceful fallback when the Storage API is unavailable. - persistence.js: request persistence once, after the first successful save — a heuristic that works across Firefox (user gesture context) and Chromium/Safari (data-stored context). - sw.js: bump cache version, precache new file. - index.html: load new script. --- public/index.html | 1 + public/sw.js | 3 ++- src/Application/persistence.js | 6 ++++++ src/Infrastructure/persistentStorage.js | 20 ++++++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/Infrastructure/persistentStorage.js diff --git a/public/index.html b/public/index.html index 7c6662b..1a1b07b 100644 --- a/public/index.html +++ b/public/index.html @@ -303,6 +303,7 @@ + diff --git a/public/sw.js b/public/sw.js index ed08810..3c5a17a 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,4 +1,4 @@ -const CACHE_VERSION = 'duimstok-v1'; +const CACHE_VERSION = 'duimstok-v2'; const APP_SHELL = [ './', @@ -19,6 +19,7 @@ const APP_SHELL = [ '../src/Infrastructure/utils.js', '../src/Infrastructure/geolocation.js', '../src/Infrastructure/db.js', + '../src/Infrastructure/persistentStorage.js', '../src/Infrastructure/seedOrders.js', '../src/Infrastructure/csvLoader.js', '../src/Application/state.js', diff --git a/src/Application/persistence.js b/src/Application/persistence.js index 8c8dd14..2e3e54b 100644 --- a/src/Application/persistence.js +++ b/src/Application/persistence.js @@ -1,4 +1,6 @@ (function (A, I) { + let persistenceRequested = false; + function readFormFields() { const fd = A.state.formData; fd.inspecteur = document.getElementById('inp_inspecteur').value; @@ -14,6 +16,10 @@ await I.saveInspection(A.state.formData); document.getElementById('statusSaved').textContent = 'Opgeslagen: ' + new Date().toLocaleTimeString('nl-NL'); + if (!persistenceRequested) { + persistenceRequested = true; + I.requestPersistence(); + } }; A.autoSave = function () { diff --git a/src/Infrastructure/persistentStorage.js b/src/Infrastructure/persistentStorage.js new file mode 100644 index 0000000..6486677 --- /dev/null +++ b/src/Infrastructure/persistentStorage.js @@ -0,0 +1,20 @@ +(function (I) { + function supported() { + return navigator.storage && typeof navigator.storage.persist === 'function'; + } + + I.isPersisted = async function () { + if (!supported()) return false; + return navigator.storage.persisted(); + }; + + I.requestPersistence = async function () { + if (!supported()) return false; + if (await navigator.storage.persisted()) return true; + try { + return await navigator.storage.persist(); + } catch { + return false; + } + }; +})(window.App.Infrastructure);