Changeset df9da7d in Klonkt


Ignore:
Timestamp:
07/16/2026 12:20:51 PM (8 weeks ago)
Author:
Robin <roboburr@…>
Branches:
main
Children:
72f936a
Parents:
4424af7
git-author:
Robin <roboburr@…> (07/12/2026 11:35:56 PM)
git-committer:
Robin <roboburr@…> (07/16/2026 12:20:51 PM)
Message:

Fix: PWA no longer shows stale homepage on flaky cold start

The service worker is network-first for navigations (fresh when online), but
its offline fallback served the '/' snapshot cached once at install time, so a
cold PWA launch on a slow/offline connection showed old data. Now refresh the
cached '/' on every successful homepage load, so the fallback is always the
latest seen page. Bumped cache version v16 → v17 so clients adopt the new SW.

Co-Authored-By: Claude Opus 4.8 <noreply@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/server.js

    r4424af7 rdf9da7d  
    470470  res.set('Cache-Control', 'no-cache');
    471471  res.send(`
    472 const CACHE_VERSION = 'pcms-v16-' + new Date().toISOString().split('T')[0];
     472const CACHE_VERSION = 'pcms-v17-' + new Date().toISOString().split('T')[0];
    473473self.addEventListener('install', e => {
    474474  e.waitUntil(caches.open(CACHE_VERSION).then(c => c.addAll(['/'])));
     
    495495  try { if (new URL(e.request.url).origin !== self.location.origin) return; } catch (err) { return; }
    496496  e.respondWith(
    497     fetch(e.request).catch(() => caches.match('/').then(r => r || Response.error()))
     497    fetch(e.request).then(resp => {
     498      // Network-first: always serve fresh when online. Also refresh the '/' offline
     499      // fallback with the homepage we just served, so a later cold start on a flaky or
     500      // offline connection no longer shows the stale install-time snapshot ("old data
     501      // on first PWA load").
     502      try {
     503        if (resp && resp.ok && new URL(e.request.url).pathname === '/') {
     504          const copy = resp.clone();
     505          e.waitUntil(caches.open(CACHE_VERSION).then(c => c.put('/', copy)).catch(() => {}));
     506        }
     507      } catch (err) { /* ignore cache refresh failures */ }
     508      return resp;
     509    }).catch(() => caches.match('/').then(r => r || Response.error()))
    498510  );
    499511});
Note: See TracChangeset for help on using the changeset viewer.