Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option forceRevalidateOnReconnect to hard-revalidate when regaining a network connection #2776

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions _internal/src/types.ts
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
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
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