mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
feat(pwa): real offline read value via SW data cache + query persistence
An installed PWA opened offline rendered empty skeletons forever: the service worker hard-excluded every /api/* path and there was no client query persistence. Close both gaps for read value. Service worker: add a stale-while-revalidate branch for an allowlist of safe GET /api/* reads (dashboard snapshot + widgets, measurements, medications, insights, analytics, version) in a release-versioned healthlog-data cache. Two-layer refusal — a path deny guard for any auth/token/session/login segment plus a body-level secret refusal for hlk_/hlr_/sk- shapes and no-store responses — so a secret-shaped body or an auth surface can never be persisted. Mutations stay network-only. Client: add a dependency-free IndexedDB query persister that hydrates the last successful reads before the first authenticated paint, build-version and age gated, debounced on cache change, with an auth/session/admin/token denylist. Cleared on logout alongside the SW page and data caches so a shared device never leaks one account's data. Offline banner now states it is showing last-synced data when a cached snapshot is present. Bump release to v1.18.6.
This commit is contained in:
1 parent
dd44ddeb70
commit
bb08aa42dc
17 files changed
+660
-15
No files matched your search
@@ -13,6 +13,7 @@ import { join } from "node:path";
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
import { OfflineBanner } from "../offline-banner";
|
||||
@@ -27,8 +28,14 @@ function loadMessages(locale: string): { offlineBanner: { message: string } } {
|
||||
}
|
||||
|
||||
function render(node: React.ReactNode, locale: "en" | "de" = "en") {
|
||||
// v1.18.6 — the banner now reads the cached dashboard snapshot to decide
|
||||
// whether to append the "showing last synced data" hint, so it needs a
|
||||
// QueryClient in context.
|
||||
const queryClient = new QueryClient();
|
||||
return renderToStaticMarkup(
|
||||
<I18nProvider initialLocale={locale}>{node}</I18nProvider>,
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<I18nProvider initialLocale={locale}>{node}</I18nProvider>
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { WifiOff } from "lucide-react";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
|
||||
/**
|
||||
* v1.4.43 QoL (M5) — slim banner that surfaces "you're offline" while
|
||||
@@ -25,6 +27,7 @@ import { useTranslations } from "@/lib/i18n/context";
|
||||
*/
|
||||
export function OfflineBanner() {
|
||||
const { t } = useTranslations();
|
||||
const queryClient = useQueryClient();
|
||||
// Initialise to `true` so the banner stays hidden during SSR + the
|
||||
// first client render. The effect below flips it true/false after
|
||||
// mount based on `navigator.onLine`. (We do NOT read navigator on
|
||||
@@ -62,6 +65,12 @@ export function OfflineBanner() {
|
||||
|
||||
if (isOnline) return null;
|
||||
|
||||
// v1.18.6 — when an offline install is showing last-synced data (the SW's
|
||||
// allowlisted read cache + the IndexedDB query persister hydrated the
|
||||
// dashboard snapshot), say so honestly rather than implying a blank screen.
|
||||
const hasCachedData =
|
||||
queryClient.getQueryData(queryKeys.dashboardSnapshot()) != null;
|
||||
|
||||
return (
|
||||
<div
|
||||
role="status"
|
||||
@@ -70,7 +79,10 @@ export function OfflineBanner() {
|
||||
className="bg-warning/15 border-warning/40 text-foreground flex items-center justify-center gap-2 border-b px-3 py-2 text-xs"
|
||||
>
|
||||
<WifiOff className="h-3.5 w-3.5 shrink-0" aria-hidden="true" />
|
||||
<span className="text-center">{t("offlineBanner.message")}</span>
|
||||
<span className="text-center">
|
||||
{t("offlineBanner.message")}
|
||||
{hasCachedData ? ` ${t("offlineBanner.showingCached")}` : ""}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -23,6 +23,12 @@ import { ServiceWorkerRegistrar } from "@/components/service-worker-registrar";
|
||||
import { isDashboardSnapshotEnabled } from "@/lib/dashboard/snapshot-flag";
|
||||
import { prefetchDashboardSnapshot } from "@/lib/queries/use-dashboard-snapshot";
|
||||
import { prefetchMedicationsList } from "@/lib/queries/prefetch-medications";
|
||||
import {
|
||||
restorePersistedQueryCache,
|
||||
startPersistingQueryCache,
|
||||
} from "@/lib/pwa/query-persister";
|
||||
|
||||
const SHELL_VERSION = process.env.NEXT_PUBLIC_APP_VERSION ?? "";
|
||||
|
||||
// ── Theme Context ────────────────────────────────────
|
||||
|
||||
@@ -147,6 +153,31 @@ function DashboardSnapshotPreloader() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// ── Offline query persistence ────────────────────────
|
||||
//
|
||||
// v1.18.6 — hydrate the last-synced query cache from IndexedDB before the
|
||||
// first authenticated paint, then debounce-persist successful reads back.
|
||||
// Combined with the service worker's allowlisted stale-while-revalidate API
|
||||
// branch, an installed PWA opened offline renders last-known data instead of
|
||||
// empty skeletons. Build-version + age gated; cleared on logout.
|
||||
function QueryPersistenceBridge() {
|
||||
const queryClient = useQueryClient();
|
||||
useEffect(() => {
|
||||
let stop: (() => void) | undefined;
|
||||
let cancelled = false;
|
||||
void restorePersistedQueryCache(queryClient, SHELL_VERSION).finally(() => {
|
||||
if (!cancelled) {
|
||||
stop = startPersistingQueryCache(queryClient, SHELL_VERSION);
|
||||
}
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
stop?.();
|
||||
};
|
||||
}, [queryClient]);
|
||||
return null;
|
||||
}
|
||||
|
||||
// ── Root Providers ───────────────────────────────────
|
||||
|
||||
export function Providers({
|
||||
@@ -181,6 +212,7 @@ export function Providers({
|
||||
initialMessages={initialMessages}
|
||||
>
|
||||
<AppSettingsProvider>
|
||||
<QueryPersistenceBridge />
|
||||
<DashboardSnapshotPreloader />
|
||||
{children}
|
||||
<Toaster position="bottom-right" richColors />
|
||||
|
||||
Reference in new issue
Block a user