Skip to content

Commit bee380e

Browse files
committed
Add option forceRevalidateOnReconnect to hard-revalidate when regaining a network connection
1 parent 41b0613 commit bee380e

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

_internal/src/types.ts

+10
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ export interface PublicConfiguration<
7272
* @defaultValue 5000
7373
*/
7474
focusThrottleInterval: number
75+
/**
76+
* only revalidate once during a time span in milliseconds
77+
* @defaultValue 5000
78+
*/
79+
reconnectThrottleInterval: number
7580
/**
7681
* dedupe requests with the same key in this time span in milliseconds
7782
* @defaultValue 2000
@@ -116,6 +121,11 @@ export interface PublicConfiguration<
116121
* @link https://swr.vercel.app/docs/revalidation#disable-automatic-revalidations
117122
*/
118123
revalidateIfStale: boolean
124+
/**
125+
* ignore `dedupingInterval` (but not `reconnectThrottleInterval`) when revalidating after regaining a network connection
126+
* @defaultValue false
127+
*/
128+
forceRevalidateOnReconnect: boolean
119129
/**
120130
* retry when fetcher has an error
121131
* @defaultValue true

_internal/src/utils/config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ export const defaultConfig: FullConfiguration = mergeObjects(
5858
revalidateOnFocus: true,
5959
revalidateOnReconnect: true,
6060
revalidateIfStale: true,
61+
forceRevalidateOnReconnect: false,
6162
shouldRetryOnError: true,
6263

6364
// timeouts
6465
errorRetryInterval: slowConnection ? 10000 : 5000,
6566
focusThrottleInterval: 5 * 1000,
67+
reconnectThrottleInterval: 5 * 1000,
6668
dedupingInterval: 2 * 1000,
6769
loadingTimeout: slowConnection ? 5000 : 3000,
6870

core/src/use-swr.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -577,15 +577,16 @@ export const useSWRHandler = <Data = any, Error = any>(
577577
// Expose revalidators to global event listeners. So we can trigger
578578
// revalidation from the outside.
579579
let nextFocusRevalidatedAt = 0
580+
let nextReconnectRevalidatedAt = 0
580581
const onRevalidate = (
581582
type: RevalidateEvent,
582583
opts: {
583584
retryCount?: number
584585
dedupe?: boolean
585586
} = {}
586587
) => {
588+
const now = Date.now()
587589
if (type == revalidateEvents.FOCUS_EVENT) {
588-
const now = Date.now()
589590
if (
590591
getConfig().revalidateOnFocus &&
591592
now > nextFocusRevalidatedAt &&
@@ -595,8 +596,18 @@ export const useSWRHandler = <Data = any, Error = any>(
595596
softRevalidate()
596597
}
597598
} else if (type == revalidateEvents.RECONNECT_EVENT) {
598-
if (getConfig().revalidateOnReconnect && isActive()) {
599-
softRevalidate()
599+
if (
600+
getConfig().revalidateOnReconnect &&
601+
now > nextReconnectRevalidatedAt &&
602+
isActive()
603+
) {
604+
nextReconnectRevalidatedAt = now + getConfig().reconnectThrottleInterval
605+
if (getConfig().forceRevalidateOnReconnect) {
606+
return revalidate()
607+
}
608+
else {
609+
softRevalidate()
610+
}
600611
}
601612
} else if (type == revalidateEvents.MUTATE_EVENT) {
602613
return revalidate()

0 commit comments

Comments
 (0)