The iOS app opens offline: the page is kept however it was loaded (#506)

The phone in airplane mode opened to "Cannot reach ipodderx.sdf1.net".
The service worker kept the page only when a navigation passed through
it, and the app's own load from Swift may not, so it had no page to
answer with. The page now asks the worker to keep / on every load, and
the app, when a load fails for want of a connection, tries once more
from the web view's cache (returnCacheDataDontLoad) before the alert.
The Swift is not compiled here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RqkrVCCH8YcP2Y3i8VNYWA
This commit is contained in:
2026-11-29 13:27:56 -05:00
parent 99ce2dbc03
commit 0bb7437541
6 changed files with 42 additions and 2 deletions

View File

@@ -27,4 +27,5 @@ The iOS app, `ios/`. Format: [Keep a Changelog](https://keepachangelog.com/en/1.
### Fixed ### Fixed
- The app opens with no connection instead of saying it cannot reach the server: a load that fails for want of a connection is tried again from what the app last loaded (#506).
- A question the page asks before going ahead (deleting a file, sharing an address that carries your access) is shown with Cancel and OK, rather than answered Cancel at once with nothing shown (#453). - A question the page asks before going ahead (deleting a file, sharing an address that carries your access) is shown with Cancel and OK, rather than answered Cancel at once with nothing shown (#453).

View File

@@ -136,8 +136,27 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKUIDeleg
present(alert, animated: true) present(alert, animated: true)
} }
/// The load from the web view's own cache has been tried since the last that worked.
private var triedCache = false
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
triedCache = false
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!,
withError error: Error) { withError error: Error) {
// No connection: the page as last loaded, from the web view's cache, which then reads
// what the service worker kept. The app's own load may not go through the worker, so
// offline it failed and said the server could not be reached (#506).
let offline: Set<URLError.Code> = [.notConnectedToInternet, .cannotConnectToHost, .cannotFindHost,
.timedOut, .networkConnectionLost, .dnsLookupFailed,
.dataNotAllowed, .internationalRoamingOff]
if !triedCache, let code = (error as? URLError)?.code, offline.contains(code), let url = Server.url {
triedCache = true
webView.load(URLRequest(url: url, cachePolicy: .returnCacheDataDontLoad))
return
}
triedCache = false
let host = Server.url?.host ?? "the server" let host = Server.url?.host ?? "the server"
let alert = UIAlertController(title: "Cannot reach \(host)", let alert = UIAlertController(title: "Cannot reach \(host)",
message: error.localizedDescription, preferredStyle: .alert) message: error.localizedDescription, preferredStyle: .alert)

View File

@@ -109,6 +109,16 @@ test('a page older than the script the server sends is loaded again, not run wit
expect(await kept()).not.toContain('/app.js?v=000000000000'); expect(await kept()).not.toContain('/app.js?v=000000000000');
}); });
test('the page has the worker keep it however it was loaded, for the iOS app (#506)', async ({ page }) => {
await controlled(page);
const has = () => page.evaluate(async () => !!(await (await caches.open('ipx-page')).match('/')));
// As if the load had not come through the worker, which the app's own load does not.
await page.evaluate(async () => { await (await caches.open('ipx-page')).delete('/'); });
expect(await has()).toBe(false);
await page.evaluate(() => navigator.serviceWorker.controller!.postMessage({ keepPage: true }));
await expect.poll(has).toBe(true);
});
test('online, a picture in an item\'s text still comes from its publisher, through the worker', async ({ page }) => { test('online, a picture in an item\'s text still comes from its publisher, through the worker', async ({ page }) => {
await controlled(page); await controlled(page);
await page.locator('#feedlist .feed', { hasText: 'Offline Blog' }).click({ timeout: 20_000 }); await page.locator('#feedlist .feed', { hasText: 'Offline Blog' }).click({ timeout: 20_000 });

View File

@@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- The page is kept for opening offline however it was loaded, so the iOS app opens with no connection too (#506).
- On a phone, an episode's row is one line like the rest: its length, which took a line of its own, is in the reader, and the date sits on the title's line (#500). - On a phone, an episode's row is one line like the rest: its length, which took a line of its own, is in the reader, and the date sits on the title's line (#500).
- On an iPhone, the reader rebuilds its scrolling each time an item opens, and again after a drag that did not move it, so a freshly opened item scrolls on the first try (#450). - On an iPhone, the reader rebuilds its scrolling each time an item opens, and again after a drag that did not move it, so a freshly opened item scrolls on the first try (#450).
- The count under All Subscriptions counts only its own unread items, not those of feeds hidden from it, so it agrees with the header (#499). - The count under All Subscriptions counts only its own unread items, not those of feeds hidden from it, so it agrees with the header (#499).

View File

@@ -5,6 +5,8 @@
// it was. // it was.
const worker=('serviceWorker' in navigator) ? navigator.serviceWorker : null; const worker=('serviceWorker' in navigator) ? navigator.serviceWorker : null;
worker?.register('/sw.js').catch(()=>{}); worker?.register('/sw.js').catch(()=>{});
// The worker keeps this page for opening offline, however it was loaded (#506).
void worker?.ready.then(r=>r.active?.postMessage({keepPage:true}));
const toWorker=(msg: object)=>worker?.controller?.postMessage(msg); const toWorker=(msg: object)=>worker?.controller?.postMessage(msg);
/// What waited for the connection goes now: on its return, and when the live stream comes back. /// What waited for the connection goes now: on its return, and when the live stream comes back.
function flushOutbox(){ toWorker({flush:true}); } function flushOutbox(){ toWorker({flush:true}); }

View File

@@ -53,8 +53,11 @@ sw.addEventListener('fetch', e => {
}); });
sw.addEventListener('message', e => { sw.addEventListener('message', e => {
const m = e.data as {flush?: boolean, pictures?: string[]} | null; const m = e.data as {flush?: boolean, pictures?: string[], keepPage?: boolean} | null;
if (m?.flush) e.waitUntil(flush()); if (m?.flush) e.waitUntil(flush());
// Asked by the page each time it loads, since what loaded it may not have come through here:
// the iOS app's own load of the page never did, so the worker had no page to open offline (#506).
if (m?.keepPage) e.waitUntil(fetch('/', { credentials: 'same-origin', redirect: 'manual' }).then(keepPage, () => {}));
if (Array.isArray(m?.pictures)) e.waitUntil(keepPictures(m.pictures)); if (Array.isArray(m?.pictures)) e.waitUntil(keepPictures(m.pictures));
}); });
@@ -63,13 +66,17 @@ sw.addEventListener('message', e => {
async function page(req: Request): Promise<Response> { async function page(req: Request): Promise<Response> {
try { try {
const r = await fetch(req); const r = await fetch(req);
if (r.ok && r.type === 'basic' && new URL(req.url).pathname === '/') (await caches.open(PAGE)).put('/', r.clone()); if (new URL(req.url).pathname === '/') await keepPage(r);
return r; return r;
} catch { } catch {
return (await caches.match('/', { cacheName: PAGE })) || Response.error(); return (await caches.match('/', { cacheName: PAGE })) || Response.error();
} }
} }
async function keepPage(r: Response) {
if (r.ok && r.type === 'basic') await (await caches.open(PAGE)).put('/', r.clone());
}
async function hashed(req: Request, client: string): Promise<Response> { async function hashed(req: Request, client: string): Promise<Response> {
const kept = await caches.match(req, { cacheName: PAGE }); const kept = await caches.match(req, { cacheName: PAGE });
if (kept) return kept; if (kept) return kept;