Skip to content

Commit

Permalink
Add option forceRevalidateOnReconnect to hard-revalidate when regaini…
Browse files Browse the repository at this point in the history
…ng a network connection
  • Loading branch information
notuxic committed Sep 6, 2023
1 parent 4d20bff commit 6557713
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
10 changes: 10 additions & 0 deletions _internal/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ export interface PublicConfiguration<
* @defaultValue 5000
*/
focusThrottleInterval: number
/**
* only revalidate once during a time span in milliseconds
* @defaultValue 5000
*/
reconnectThrottleInterval: number
/**
* dedupe requests with the same key in this time span in milliseconds
* @defaultValue 2000
Expand Down Expand Up @@ -116,6 +121,11 @@ export interface PublicConfiguration<
* @link https://swr.vercel.app/docs/revalidation#disable-automatic-revalidations
*/
revalidateIfStale: boolean
/**
* ignore `dedupingInterval` (but not `reconnectThrottleInterval`) when revalidating after regaining a network connection
* @defaultValue false
*/
forceRevalidateOnReconnect: boolean
/**
* retry when fetcher has an error
* @defaultValue true
Expand Down
2 changes: 2 additions & 0 deletions _internal/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ export const defaultConfig: FullConfiguration = mergeObjects(
revalidateOnFocus: true,
revalidateOnReconnect: true,
revalidateIfStale: true,
forceRevalidateOnReconnect: false,
shouldRetryOnError: true,

// timeouts
errorRetryInterval: slowConnection ? 10000 : 5000,
focusThrottleInterval: 5 * 1000,
reconnectThrottleInterval: 5 * 1000,
dedupingInterval: 2 * 1000,
loadingTimeout: slowConnection ? 5000 : 3000,

Expand Down
17 changes: 14 additions & 3 deletions core/src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,15 +577,16 @@ export const useSWRHandler = <Data = any, Error = any>(
// Expose revalidators to global event listeners. So we can trigger
// revalidation from the outside.
let nextFocusRevalidatedAt = 0
let nextReconnectRevalidatedAt = 0
const onRevalidate = (
type: RevalidateEvent,
opts: {
retryCount?: number
dedupe?: boolean
} = {}
) => {
const now = Date.now()
if (type == revalidateEvents.FOCUS_EVENT) {
const now = Date.now()
if (
getConfig().revalidateOnFocus &&
now > nextFocusRevalidatedAt &&
Expand All @@ -595,8 +596,18 @@ export const useSWRHandler = <Data = any, Error = any>(
softRevalidate()
}
} else if (type == revalidateEvents.RECONNECT_EVENT) {
if (getConfig().revalidateOnReconnect && isActive()) {
softRevalidate()
if (
getConfig().revalidateOnReconnect &&
now > nextReconnectRevalidatedAt &&
isActive()
) {
nextReconnectRevalidatedAt = now + getConfig().reconnectThrottleInterval
if (getConfig().forceRevalidateOnReconnect) {
return revalidate()
}
else {
softRevalidate()
}
}
} else if (type == revalidateEvents.MUTATE_EVENT) {
return revalidate()
Expand Down

0 comments on commit 6557713

Please sign in to comment.