Skip to content

fix(immutable): respect a hook-level refreshInterval - #4324

Open
vjymisal0 wants to merge 3 commits into
vercel:mainfrom
vjymisal0:fix-immutable-hook-refresh-interval
Open

vjymisal0 wants to merge 3 commits into
vercel:mainfrom
vjymisal0:fix-immutable-hook-refresh-interval

Conversation

@vjymisal0

Copy link
Copy Markdown

Fixes #4322

Problem

#4208 added config.refreshInterval = 0 to the immutable middleware so useSWRImmutable would ignore a refreshInterval inherited from a parent <SWRConfig>.

Middleware runs after hook options and the context config have been merged into a single object, so at that point the two sources are indistinguishable — the line disables both. A refreshInterval passed directly to the hook has silently stopped working since 2.4.0:

// 2.3.x: polls every second. >= 2.4.0: fetches once, interval discarded.
useSWRImmutable('/api/status', fetcher, { refreshInterval: 1000 })

Fix

Record whether refreshInterval came from the hook itself before the merge happens, using a symbol on the config object, and only clear the value in the middleware when that marker is absent.

useSWRImmutable previously used the generic withMiddleware helper; it now inlines the same normalize-and-append-middleware logic so it can set the marker. withMiddleware itself is untouched, so other middleware users are unaffected. The symbol survives SWR's config merging (mergeObjects is object spread, which copies symbol keys).

Behavior after this change:

Tests

Added two regression tests under describe('issue #4322'): a hook-level refreshInterval polls, and a hook-level value overrides a global one. Both fail on the current code (git stash on the source file → exactly these 2 fail) and pass with the fix.

Verification

  • pnpm jest test/use-swr-immutable.test.tsx — 9/9 pass (7 existing + 2 new).
  • pnpm types:check and pnpm test-typing pass.
  • pnpm oxlint and pnpm oxfmt --check clean on the changed files.
  • Full pnpm jest: 389 pass, 1 failure in test/use-swr-remote-mutation.test.tsx ("should prevent race condition if triggered multiple times"). That failure reproduces identically on unmodified main in this environment (verified via git stash) — a pre-existing timing flake, unrelated to this change.

vercel#4208 cleared refreshInterval inside the immutable middleware, but
middleware runs after hook options and the context config have been
merged, so it could not tell the two apart and disabled both.

Record whether refreshInterval came from the hook itself before that
merge, and only clear the value when it was inherited from a parent
<SWRConfig>.

Fixes vercel#4322
@vjymisal0
vjymisal0 force-pushed the fix-immutable-hook-refresh-interval branch from 70ba4df to 446e700 Compare September 21, 2026 06:37

This branch has not been deployed

No deployments
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.

useSWRImmutable ignores hook-level refreshInterval since 2.4.0 (regression of #4208)

1 participant