Repository navigation
Offline Hybrid Mode: use the downloaded copy first on slow connections #3066
|
Related: #1135 (force offline, selective storage), #1580 (offline UX), #3051 (bundle and places reads spend about 90% of their time in the country lookup) The problemOffline mode works well with no signal and with good signal. In between, one bar is worse than no bars: with no signal the app opens the stored copy instantly, but on a weak or patchy mobile connection, which is common when travelling, it waits on the server first. It also costs money. Travellers often rely on travel eSIMs with small data caps and a high price per GB, and every trip open downloads data the phone already has. Right now, whenever the phone reports a connection, a trip loads from the server even if the whole trip is already stored on the device. The trip page stays on the loading screen until every request finishes or fails. Documents behave the same way: opening one downloads it again even when the copy saved for offline use is already there. On a slow link that can mean waiting a long time for a boarding pass or hotel confirmation that's already on the phone. #3051 makes this worse: the server itself is slow to build the trip bundle and the places list. Force offline mode avoids the wait, but then nothing updates. It also sits under Settings → Offline, so switching it on and off several times a day isn't practical. What exists today (v5.1.3)I read through the client to make sure I'm not asking for something that's already there:
The proposalIn short, as @2SinceJuly put it below: the stored copy should be a latency floor, not only an error floor. Trust the stored copy while it's currentWhen the app opens a trip that's stored on the device:
Document version handlingDocuments need no version check of their own: a document's ID already acts as its version number. Once a file is stored, its content never changes. TREK has no way to replace a file's content. Editing a file only changes its description and links, and every upload gets a new file and a new download URL. Even document sync with Paperless, Papra or Synology Drive follows this: when a document changes in the external store, TREK adds the new revision as a new file with a new ID, moves its links over and moves the old one to the trash. So a stored document with the same ID is always identical to the server's copy, and it opens from the device whenever it's there. A deleted document drops out of the list when the trip data next updates. If editing a file's content in place is ever added, it would need to keep this rule (a new ID for new content) or give each file its own version. Version tracking suggestionA per-trip counter on the server that goes up on every change to the trip. Every trip change already goes through the realtime broadcast, so that may be the one place where the counter goes up. The client stores the version it last downloaded. The server reports the current version in three places:
That's only one way to do it, though. A last-changed timestamp, an ETag on the bundle, or something that fits the sync design better would work just as well, and I'd go with whatever you prefer. Benefits
Trade-off: a small change downloads the whole tripIf someone moves an event by an hour, the version goes up and the next open downloads all of the trip's data again, not just the changed event. That's acceptable for a first version, because the download is small:
Rollout planEither of these would work, and I'm happy with whichever you prefer. Option A: no new setting. Roll the behaviour out PR by PR to everyone. Each PR below is safe on its own: a stored document is always current because its ID is its version, the short timeout always ends up showing the server's data, and the version check only skips downloads that would have returned the same data. There's no new option to explain, and Force offline stays as it is. Option B: a mode dropdown, if you'd rather make this explicit to users. Replace the "Force offline mode" switch with a dropdown with three modes:
"Hybrid" is a placeholder name; "Offline first" or "Smart" might read better. With the dropdown, existing installs keep today's behaviour. A device with force offline on starts in "Force offline", and every other device starts in "Online preferred". Hybrid would ship as an option first, so the people who want it can try it while everyone else is unaffected. Once it has been well tested, it could become the default, if you agree. The dropdown would be added in the first PR that changes behaviour, rather than as a PR of its own. Proposed implementation planOne change per pull request. The first two need no server change and fix most of the slow-link wait on their own.
Implementation notes (from @2SinceJuly's comment below):
If the direction sounds right, I'll pitch an implementation in |
Replies: 3 comments 3 replies
|
Your reading of What the current policy actually is. if (isEffectivelyOffline()) return cacheFn()
try { return await onlineFn() } catch (err) { if (isNetworkError(err)) return cacheFn(); throw err }Two properties follow, and they're both the problem you describe:
That second point is worth stating explicitly in the issue, because it inverts the intuition — the app is best at zero signal and worst at one bar. The change I'd propose: stale-while-revalidate, not cache-first. Cache-first would fix the latency but break the freshness guarantee you explicitly want to keep ("Force offline mode avoids the wait, but then nothing updates"). Stale-while-revalidate gives both: export async function cacheThenRevalidate<T>(
cacheFn: () => Promise<T | undefined>,
onlineFn: () => Promise<T>,
onFresh: (value: T) => void,
): Promise<T> {
const cached = await cacheFn()
if (cached !== undefined) {
// Paint immediately, then let the caller refresh in place.
void onlineFn().then(onFresh).catch(() => { /* keep the cached view */ })
return cached
}
return onlineFn()
}The important detail is
A cheaper intermediate step that gets most of the benefit: keep Two implementation notes from reading the file:
I'd frame the issue this way: the ask isn't "cache first", it's "the cache should be a latency floor, not only an error floor". That phrasing makes the freshness objection answerable and keeps the change scoped to the reads that need it. |
|
@mhlas7 — the fix that fits the existing code is one extra branch in If that's the direction you want to file, please mark the comment above as the answer — "cache should be a latency floor" is the framing that makes the freshness objection answerable. If you'd rather keep it as an issue than implement it, the phrasing in the comment is the one I'd use in the issue body. |
|
Thanks @mhlas7 for the thorough write-up, and @2SinceJuly for the "latency floor" framing! The first piece lands in the next release: a document that's stored for offline use now opens from the device after about five seconds when the connection stalls, instead of loading forever. HTTP errors still show up as before, and without a stored copy nothing changes. The larger parts (opening trips from the stored copy first, a version per trip) would touch the offline sync core, so we're not planning them for now. |
Thanks @mhlas7 for the thorough write-up, and @2SinceJuly for the "latency floor" framing! The first piece lands in the next release: a document that's stored for offline use now opens from the device after about five seconds when the connection stalls, instead of loading forever. HTTP errors still show up as before, and without a stored copy nothing changes. The larger parts (opening trips from the stored copy first, a version per trip) would touch the offline sync core, so we're not planning them for now.