File tree 3 files changed +26
-3
lines changed
3 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -72,6 +72,11 @@ export interface PublicConfiguration<
72
72
* @defaultValue 5000
73
73
*/
74
74
focusThrottleInterval : number
75
+ /**
76
+ * only revalidate once during a time span in milliseconds
77
+ * @defaultValue 5000
78
+ */
79
+ reconnectThrottleInterval : number
75
80
/**
76
81
* dedupe requests with the same key in this time span in milliseconds
77
82
* @defaultValue 2000
@@ -116,6 +121,11 @@ export interface PublicConfiguration<
116
121
* @link https://swr.vercel.app/docs/revalidation#disable-automatic-revalidations
117
122
*/
118
123
revalidateIfStale : boolean
124
+ /**
125
+ * ignore `dedupingInterval` (but not `reconnectThrottleInterval`) when revalidating after regaining a network connection
126
+ * @defaultValue false
127
+ */
128
+ forceRevalidateOnReconnect : boolean
119
129
/**
120
130
* retry when fetcher has an error
121
131
* @defaultValue true
Original file line number Diff line number Diff line change @@ -58,11 +58,13 @@ export const defaultConfig: FullConfiguration = mergeObjects(
58
58
revalidateOnFocus : true ,
59
59
revalidateOnReconnect : true ,
60
60
revalidateIfStale : true ,
61
+ forceRevalidateOnReconnect : false ,
61
62
shouldRetryOnError : true ,
62
63
63
64
// timeouts
64
65
errorRetryInterval : slowConnection ? 10000 : 5000 ,
65
66
focusThrottleInterval : 5 * 1000 ,
67
+ reconnectThrottleInterval : 5 * 1000 ,
66
68
dedupingInterval : 2 * 1000 ,
67
69
loadingTimeout : slowConnection ? 5000 : 3000 ,
68
70
Original file line number Diff line number Diff line change @@ -577,15 +577,16 @@ export const useSWRHandler = <Data = any, Error = any>(
577
577
// Expose revalidators to global event listeners. So we can trigger
578
578
// revalidation from the outside.
579
579
let nextFocusRevalidatedAt = 0
580
+ let nextReconnectRevalidatedAt = 0
580
581
const onRevalidate = (
581
582
type : RevalidateEvent ,
582
583
opts : {
583
584
retryCount ?: number
584
585
dedupe ?: boolean
585
586
} = { }
586
587
) => {
588
+ const now = Date . now ( )
587
589
if ( type == revalidateEvents . FOCUS_EVENT ) {
588
- const now = Date . now ( )
589
590
if (
590
591
getConfig ( ) . revalidateOnFocus &&
591
592
now > nextFocusRevalidatedAt &&
@@ -595,8 +596,18 @@ export const useSWRHandler = <Data = any, Error = any>(
595
596
softRevalidate ( )
596
597
}
597
598
} 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
+ }
600
611
}
601
612
} else if ( type == revalidateEvents . MUTATE_EVENT ) {
602
613
return revalidate ( )
You can’t perform that action at this time.
0 commit comments