79 lines
3.2 KiB
JavaScript
79 lines
3.2 KiB
JavaScript
|
|
// citti offline support: register the SW, and make mutations survive no-net.
|
||
|
|
// window.cittiFetch(url, opts) tries the network; if it fails (offline), it
|
||
|
|
// queues the POST in localStorage, resolves optimistically, and replays the
|
||
|
|
// queue in order when the connection returns.
|
||
|
|
(function () {
|
||
|
|
if ('serviceWorker' in navigator) {
|
||
|
|
navigator.serviceWorker.register('/sw.js').catch(function (e) {
|
||
|
|
console.warn('[citti] SW registration failed', e);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
var QKEY = 'citti_queue_v1';
|
||
|
|
function getQ() { try { return JSON.parse(localStorage.getItem(QKEY) || '[]'); } catch (e) { return []; } }
|
||
|
|
function setQ(q) { try { localStorage.setItem(QKEY, JSON.stringify(q)); } catch (e) {} }
|
||
|
|
|
||
|
|
window.cittiFetch = function (url, opts) {
|
||
|
|
opts = opts || {};
|
||
|
|
var method = (opts.method || 'GET').toUpperCase();
|
||
|
|
return fetch(url, opts).then(function (r) {
|
||
|
|
if (!r.ok) throw new Error('status ' + r.status);
|
||
|
|
return r;
|
||
|
|
}).catch(function () {
|
||
|
|
if (method !== 'GET') {
|
||
|
|
var q = getQ();
|
||
|
|
q.push({
|
||
|
|
url: url, method: method,
|
||
|
|
headers: opts.headers || {},
|
||
|
|
body: typeof opts.body === 'string' ? opts.body : null
|
||
|
|
});
|
||
|
|
setQ(q);
|
||
|
|
render();
|
||
|
|
}
|
||
|
|
// optimistic: let the UI proceed as if saved
|
||
|
|
return new Response(JSON.stringify({ ok: true, queued: true }),
|
||
|
|
{ status: 200, headers: { 'Content-Type': 'application/json' } });
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
var flushing = false;
|
||
|
|
function flush() {
|
||
|
|
if (flushing || !navigator.onLine) { render(); return; }
|
||
|
|
var q = getQ();
|
||
|
|
if (!q.length) { render(); return; }
|
||
|
|
flushing = true;
|
||
|
|
var next = q[0];
|
||
|
|
fetch(next.url, { method: next.method, headers: next.headers, body: next.body })
|
||
|
|
.then(function (r) {
|
||
|
|
if (!r.ok) throw new Error('status ' + r.status);
|
||
|
|
var qq = getQ(); qq.shift(); setQ(qq);
|
||
|
|
flushing = false; render();
|
||
|
|
if (getQ().length) flush();
|
||
|
|
})
|
||
|
|
.catch(function () { flushing = false; render(); });
|
||
|
|
}
|
||
|
|
|
||
|
|
function render() {
|
||
|
|
var n = getQ().length;
|
||
|
|
var el = document.getElementById('citti-sync');
|
||
|
|
if (!el) {
|
||
|
|
el = document.createElement('div');
|
||
|
|
el.id = 'citti-sync';
|
||
|
|
el.style.cssText = 'position:fixed;left:50%;transform:translateX(-50%);bottom:14px;z-index:9999;'
|
||
|
|
+ 'padding:8px 16px;border-radius:20px;font:600 13px/1 system-ui,-apple-system,sans-serif;'
|
||
|
|
+ 'color:#fff;box-shadow:0 2px 10px rgba(0,0,0,.25);transition:opacity .3s;pointer-events:none';
|
||
|
|
document.body.appendChild(el);
|
||
|
|
}
|
||
|
|
var offline = !navigator.onLine;
|
||
|
|
if (offline && n) { el.textContent = '📴 Offline · ' + n + ' gemmes når du er online'; el.style.background = '#92580a'; el.style.opacity = '1'; }
|
||
|
|
else if (offline) { el.textContent = '📴 Offline'; el.style.background = '#5a5550'; el.style.opacity = '1'; }
|
||
|
|
else if (n) { el.textContent = '↻ Synkroniserer ' + n + '…'; el.style.background = '#2d6a4f'; el.style.opacity = '1'; }
|
||
|
|
else { el.style.opacity = '0'; }
|
||
|
|
}
|
||
|
|
|
||
|
|
window.addEventListener('online', function () { render(); flush(); });
|
||
|
|
window.addEventListener('offline', render);
|
||
|
|
if (document.readyState !== 'loading') { render(); flush(); }
|
||
|
|
else document.addEventListener('DOMContentLoaded', function () { render(); flush(); });
|
||
|
|
})();
|