perf(medications): stop re-sending the read a nav prefetch just made

The medications list and the compliance summary refetch on every mount,
so a take or skip logged on another device shows on return (#316). The
nav link's hover / touch / focus intent and the route-commit preload
prefetch both reads too, so every visit through a link sent each of
them twice: the prefetch, then the identical request from the mount a
few hundred milliseconds after the first answer landed.

The mount now skips its refetch only when the cached data answers a
prefetch that went to the network for this visit: within the last 10 s,
and since the route last left /medications. A direct load, a return
with an older entry, or a quick return whose prefetch found the entry
still fresh keep the refetch on mount.
This commit is contained in:
Marc-André Bombeck committed 2026-10-04 16:12:04 +02:00
1 parent 2c1369f01b
commit 23961d2280
6 files changed
+229 -18

No files matched your search

+4 -1
View File
@@ -55,6 +55,7 @@ import {
Wrench,
} from "lucide-react";
import { apiGet } from "@/lib/api/api-fetch";
import { refetchMedicationsOnMount } from "@/lib/queries/prefetch-medications";
import { useMedicationComplianceSummaryAll } from "@/lib/queries/use-medication-compliance-summary";
import { useMedicationListLayout } from "@/lib/queries/use-medication-list-layout";
import { applyMedicationOrder } from "@/lib/medications/medication-order";
@@ -345,7 +346,9 @@ export default function MedicationsPageClient() {
// client cache can't know to invalidate a cross-device write. "always"
// makes a return-visit fetch fresh; the per-user server cache keeps it
// cheap. Mutation-driven invalidation still covers same-tab writes.
refetchOnMount: "always",
// A prefetch this same navigation fired (nav-link intent, route commit)
// already is that fresh fetch, so it is not sent twice.
refetchOnMount: refetchMedicationsOnMount("list"),
});
// v1.30.1 M12 — pull-to-refresh parity with labs/checkups/mood/measurements.
+3 -1
View File
@@ -39,7 +39,9 @@ import MedicationsPageClient from "./page-client";
* event, so the list must re-verify on every mount. With the prefetch in
* place the hydrated cards paint immediately and that refetch runs in the
* background — the "empty then fills" flash is gone WITHOUT dropping the
* freshness guarantee.
* freshness guarantee. The one exception is data a prefetch fired by this
* same navigation just fetched (`refetchMedicationsOnMount`): that already
* is the re-verification, so it is not sent twice.
* - Record identity: the session comes from `getUnswitchedSession()`, which
* answers null while the browser is acting on somebody else's record.
* `/api/medications` IS delegable, so the client cell corrects itself on its