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

Conversation

notuxic
Copy link

@notuxic notuxic commented Sep 6, 2023

TLDR

Adds an option forceRevalidateOnReconnect, which when set to true, triggers a revalidation after reconnecting even when dedupingInterval hasn't passed yet. To compensate spotty network connections, an option reconnectThrottleInterval allows to throttle further revalidations for this event.

Background

I'm currently working on an app using SWR with a high dedupingInterval (30 seconds). Depending on network availability, the fetcher fetches data either from network or a local cache.

Now if users navigate to a screen while offline, and then turn on the network (for example because they see the "no-network" placeholder when there's no cached data), the data won't be revalidated since the 30 seconds of the dedupingInterval haven't passed yet.

forceRevalidateOnReconnect

With the new option forceRevalidateOnReconnect set to true (defaulting to false), SWR will call revalidate() instead of softRevalidate() to bypass dedupingInterval, similar to mutate-events.

reconnectThrottleInterval

On spotty network connections, the above new option may trigger a lot of unwanted/unneeded revalidations. For this reason a second option reconnectThrottleInterval allows to throttle revalidations on reconnect-events, similar to the existing focusThrottleInterval for focus-events.

reconnectThrottleInterval defaults to 5000 for consistency with focusThrottleInterval.

Open questions

Default value for reconnectThrottleInterval

Currently reconnectThrottleInterval uses the same default value as focusThrottleInterval for consistency.

A default value of 0 would preserve backwards-compatible behaviour on reconnect-events, but may not be as good of a default for the feature.

Maybe the default value should be even higher, since 5 seconds is quite short in the case of spotty network connections.

Copy link

@orca-security-us orca-security-us bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 6, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit a38d67f:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration
SWR-SSR Configuration

@notuxic
Copy link
Author

notuxic commented Oct 3, 2023

ping @shuding @huozhi, requesting review, since it's been a month since opening the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant