Open the app from the cached session without waiting on the network

Bootstrap awaited api/me before showing anything, so with the server
unreachable but the device nominally online the app sat blank until the
request gave up. With a cached session it now boots immediately and
verifies api/me in the background: a 401 bounces to login, a different
identity reloads, a network failure changes nothing.
This commit is contained in:
Alexander Heldt committed 2026-10-01 15:22:18 +00:00
1 parent 533aa1cc34
commit e17856b9fe
2 files changed
+39 -14

No files matched your search

+38 -14
View File
@@ -6471,10 +6471,13 @@
location.reload();
});
// On load, ask the server who we are. A valid session boots straight into the
// app. A 401 means log in. A network failure (offline PWA) falls back to the
// last cached session so offline data stays reachable — a later sync will
// 401 and bounce to login if that session has actually gone stale.
// On load, work out who we are. With a cached session the app opens on it
// immediately and api/me is checked in the background: waiting on the
// network first meant that with no reachable server (offline, flaky signal,
// server down) the app sat blank until the request gave up, which can take a
// long time when the device thinks it is online. Without a cached session we
// have to ask: a valid session boots into the app, a 401 means log in, and a
// network failure leaves us on the login screen.
(async function bootstrap() {
// /guest/<token> bounces here with this marker when the link was already
// expired or revoked, so we can say so rather than show a sign-in form.
@@ -6484,6 +6487,16 @@
history.replaceState(null, "", location.pathname);
}
const cached = deadLink ? null : cachedUser();
if (cached) {
currentUser = cached;
applyRole();
showApp();
startApp();
verifySession(cached);
return;
}
try {
const res = await fetch("api/me");
if (res.ok) {
@@ -6494,20 +6507,31 @@
return;
}
clearUser(); // explicit 401/403: session is gone
} catch {
const cached = cachedUser();
if (cached) {
currentUser = cached;
applyRole();
showApp();
startApp();
return;
}
}
} catch { /* unreachable and nothing cached: fall through to login */ }
renderAuthMode();
showAuth({ ended: deadLink });
})();
// Background check of a session the app was opened on from cache. A network
// failure changes nothing (sync retries on its own schedule). A 401 bounces
// to login just as a sync would. A different identity — a guest link
// redeemed in a browser that remembered someone else, say — reloads so the
// app boots against the right account's data.
async function verifySession(cached) {
let res;
try { res = await fetch("api/me"); } catch { return; }
if (res.status === 401 || res.status === 403) { handleLoggedOut(); return; }
if (!res.ok) return;
let fresh;
try { fresh = await res.json(); } catch { return; }
const same = fresh.id === cached.id
&& fresh.role === cached.role
&& fresh.shareId === cached.shareId;
setUser(fresh); // also picks up e.g. a changed guest label or expiry
if (!same) { location.reload(); return; }
renderGuestBanner();
}
// One-time, role-dependent chrome. Everything else a guest sees or doesn't is
// decided when the relevant dialog opens.
function applyRole() {
+1
View File
@@ -1,4 +1,5 @@
[
{ "date": "2026-10-01", "text": "The app opens straight away when you have no connection. It used to check in with the server before showing anything, and on a weak signal or with the server out of reach that check could take a long while to give up, leaving a blank screen even though all your data was already on the phone. It now opens on the account you were last signed in to and checks in the background; if that sign-in has run out you are taken to the login screen as before" },
{ "date": "2026-09-30", "text": "The Waking → pee and Meal → poo rows are gone from the Timing panel again. With a puppy who takes lots of short naps, most wake-ups were followed by another nap before any pee, so Waking → pee was built from a handful of odd cases and was hard to make sense of. The Pees, Poos and Meals rows are unchanged" },
{ "date": "2026-09-29", "text": "The Walks chart (minutes per day) has a trend line too, with a sentence under it in the same style as the sleep one: “daily walking is up about 20m — from roughly 35m a day to 55m”, or roughly steady with the average when the days vary more than they move. A day with no walk counts as a real zero — a rest day is a real figure — but the days before the very first walk you logged are left out, so starting to track walks partway through the window doesn't read as a sudden climb. Today and days marked “not counted” are left out as on every other trend" },
{ "date": "2026-09-29", "text": "The Sleep chart on the Sleep tab has a trend line through it, like the Food (grams) chart, so you can see whether he is sleeping less as he grows — the day-to-day bars vary enough to hide a steady change. A line under the chart puts it in figures: “daily sleep is down about 1.5h — from roughly 17h a day to 15.5h”, or, when the days scatter more than they move, that it is roughly steady and what the average is. Today is left out until it's over, as are days marked “not counted” and days with no sleep logged at all, which the sentence mentions so an empty bar isn't mistaken for a night without sleep. It follows the 7 / 14 / 30 day picker like the rest of the charts" },