-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Bug report
Description / Observed Behavior
When using useSWRImmutable, the hook should disable revalidation on focus. However, if you leave the app window and go back in (triggering a window focus event), the fetcher function is called again. This is confirmed by the console.log("getItems called") inside the fetcher firing every time the app regains focus.
Expected Behavior
useSWRImmutable should implicitly set revalidateIfStale, revalidateOnFocus, and revalidateOnReconnect to false. Leaving the app and returning should not trigger the fetcher function if the cache is already populated and the SWR key has not changed.
Repro Steps / Code Example
Link to reproduction: CodeSandbox. Look inside the console! Don’t switch focus too quickly; otherwise, you won’t see it due to the deduping interval in SWR.
Additional Context
SWR version: 2.4.0
React version: 19.2.4
Update: Potential Workaround Found
I have discovered that explicitly setting the revalidation flags to false within the options object resolves the issue. Even though useSWRImmutable is supposed to handle this by default, adding these options explicitly ensures the hook works as expected:
useSWRImmutable(key, fetcher, { revalidateOnFocus: false, })