Skip to content

Unexpected fetcher call on focus when using useSWRImmutable #4225

@TudorGiuroiu

Description

@TudorGiuroiu

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, })

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions