diff --git a/ios/CHANGELOG.md b/ios/CHANGELOG.md index 8b4b81c..53ded44 100644 --- a/ios/CHANGELOG.md +++ b/ios/CHANGELOG.md @@ -27,4 +27,5 @@ The iOS app, `ios/`. Format: [Keep a Changelog](https://keepachangelog.com/en/1. ### 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). diff --git a/ios/Sources/WebViewController.swift b/ios/Sources/WebViewController.swift index fcf9b10..0d59dd3 100644 --- a/ios/Sources/WebViewController.swift +++ b/ios/Sources/WebViewController.swift @@ -136,8 +136,27 @@ final class WebViewController: UIViewController, WKNavigationDelegate, WKUIDeleg 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!, 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 = [.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 alert = UIAlertController(title: "Cannot reach \(host)", message: error.localizedDescription, preferredStyle: .alert) diff --git a/tests/ui/offline.spec.ts b/tests/ui/offline.spec.ts index 3a6ebdd..a31cec5 100644 --- a/tests/ui/offline.spec.ts +++ b/tests/ui/offline.spec.ts @@ -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'); }); +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 }) => { await controlled(page); await page.locator('#feedlist .feed', { hasText: 'Offline Blog' }).click({ timeout: 20_000 }); diff --git a/web/CHANGELOG.md b/web/CHANGELOG.md index b15aa1a..cab2501 100644 --- a/web/CHANGELOG.md +++ b/web/CHANGELOG.md @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### 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 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). diff --git a/web/src/offline.ts b/web/src/offline.ts index 4d873c1..8e23b4d 100644 --- a/web/src/offline.ts +++ b/web/src/offline.ts @@ -5,6 +5,8 @@ // it was. const worker=('serviceWorker' in navigator) ? navigator.serviceWorker : null; 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); /// What waited for the connection goes now: on its return, and when the live stream comes back. function flushOutbox(){ toWorker({flush:true}); } diff --git a/web/sw/sw.ts b/web/sw/sw.ts index f71a05c..6c9b126 100644 --- a/web/sw/sw.ts +++ b/web/sw/sw.ts @@ -53,8 +53,11 @@ sw.addEventListener('fetch', 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()); + // 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)); }); @@ -63,13 +66,17 @@ sw.addEventListener('message', e => { async function page(req: Request): Promise { try { 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; } catch { 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 { const kept = await caches.match(req, { cacheName: PAGE }); if (kept) return kept;