aboutsummaryrefslogtreecommitdiff
path: root/src/lib/shared/refresh-policy.ts
blob: d79e6b7f2555c668cecbc0a22e8ba6787054fa87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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<string, true> = {
	'/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;
}