From 031f99e626f52ea7baac9155b0cbe6e4fcfbfb31 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Sun, 18 Oct 2026 21:26:24 +0200 Subject: feat: poll server data so shared pages stay near-live Refetch the current page every 15s while the tab is visible, gated by a pure refresh policy: edit pages, hidden or offline tabs and in-flight navigations are left alone. Replaces refresh-on-refocus with auto-refresh. --- src/lib/shared/refresh-policy.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/lib/shared/refresh-policy.ts (limited to 'src/lib/shared/refresh-policy.ts') diff --git a/src/lib/shared/refresh-policy.ts b/src/lib/shared/refresh-policy.ts new file mode 100644 index 0000000..d79e6b7 --- /dev/null +++ b/src/lib/shared/refresh-policy.ts @@ -0,0 +1,31 @@ +/** + * Routes whose page data must not be refetched in the background: edit pages + * hold in-progress work, and refreshing the login form is pointless. + */ +export const EXCLUDED_ROUTES: Record = { + '/login': true, + '/grocery/[id]': true, + '/projects/[projectId]/routine/[routineId]': true, +}; + +export interface RefreshConditions { + /** `$page.route.id`; null when no route matched yet. */ + routeId: string | null | undefined; + /** Document is visible (not a backgrounded tab). */ + visible: boolean; + /** Browser reports a network connection. */ + online: boolean; + /** A navigation or form action is already in flight. */ + navigating: boolean; +} + +/** A background refetch may re-run the page load only if nothing is at stake. */ +export function shouldAutoRefresh({ + routeId, + visible, + online, + navigating, +}: RefreshConditions): boolean { + if (!routeId || routeId in EXCLUDED_ROUTES) return false; + return visible && online && !navigating; +} -- cgit v1.3