Fix iOS offline: NetworkFirst networkTimeoutSeconds + skipWaiting

On iOS, offline network requests hang instead of failing fast (navigator.onLine
even stays true), so the inventory NetworkFirst route waited forever on the hung
network attempt and never fell back to the cache -> infinite spinner offline.
Desktop browsers fail the fetch fast, which is why they worked.

- networkTimeoutSeconds: 3 on the inventory route, so it serves the cached copy
  after a few seconds when the network hangs.
- self.skipWaiting() so worker updates activate immediately (reach installed PWAs,
  incl. iOS home-screen apps, without a reinstall).

Confirmed on-device via Web Inspector: the home-screen app had the worker active,
controlling, and the inventory cached, but fetch('/api/inventory') hung (pending)
offline while navigator.onLine reported true.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-06-22 18:24:47 -06:00
parent 201df63e30
commit b3f8e0a56c
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680

View file

@ -15,6 +15,9 @@ import {registerRoute} from 'workbox-routing';
import {NetworkFirst, NetworkOnly} from 'workbox-strategies';
import {CacheableResponsePlugin} from 'workbox-cacheable-response';
// Activate a new worker immediately so fixes/updates reach installed PWAs (incl. iOS home-screen apps) without a
// reinstall, then take control of open clients.
self.skipWaiting();
clientsClaim();
// Precache the shell/JS/CSS emitted by the webpack build. This is what lets the Calculators work offline.
@ -50,6 +53,10 @@ registerRoute(
),
new NetworkFirst({
cacheName: 'wrolpi-inventory',
// iOS does not fail offline fetches fast - they hang. Without a timeout, NetworkFirst would wait forever
// on the (hung) network attempt and never fall back to the cache, leaving the page spinning offline. Give
// the network a few seconds, then serve the cached copy.
networkTimeoutSeconds: 3,
plugins: [new CacheableResponsePlugin({statuses: [200]})],
})
);