RTK Query keeps repeating a request that fails
The same failed request appears about six times in the network tabShort answer
RTK Query does not retry by itself. Repeated calls come from the retry wrapper around baseQuery, which tries 5 more times by default, or from refetch options. Set maxRetries to 0 on the endpoint, lower it globally, or call retry.fail for status codes that can never succeed.
Fix it fast
1Find out which cause you have
Find out whether your baseQuery is wrapped with retry. If it is, retries are on by default.
grep -rn "retry" src --include=*.ts --include=*.tsx --include=*.js2Match what you see, then jump to its fix
- 1
One failing call causes the first request plus five more, spaced further apart each timebaseQuery is wrapped with retry and uses the default countFix - 2
Only a mutation or a single query should fail fast; the rest may retryOne endpoint should never retryFix - 3
401, 403 or 404 responses are repeated before the error is shownErrors that can never succeed are retried tooFix - 4
Requests repeat only after you refocus the tab or reconnect to the networkRefetching, not retrying, is sending the requestFix
- 1
What does the error look like?
The same failed request appears about six times in the network tabThe error only shows after a long delay
Cause 1 of 4
baseQuery is wrapped with retry and uses the default count
1Check
The retry utility makes 5 retries after the first request, with exponential backoff starting near 600 ms. Look for a retry(...) around fetchBaseQuery.
grep -rn "retry(" src2Fix
Pass a smaller maxRetries to the wrapper, or remove the wrapper if you want no retries anywhere.
import { createApi, fetchBaseQuery, retry } from '@reduxjs/toolkit/query/react'
const baseQuery = retry(fetchBaseQuery({ baseUrl: '/api/' }), {
maxRetries: 1,
})3Verify
Trigger a failing request and count the calls in the network tab: with maxRetries 1 there are at most 2.
# browser devtools, Network tab, filter by the endpoint URLConfirmed by redux.js.org
Cause 2 of 4
One endpoint should never retry
1Check
An endpoint can override the wrapper through extraOptions. maxRetries counts retries only, not the first request.
grep -rn "extraOptions" src2Fix
Set maxRetries to 0 on that endpoint.
login: build.mutation({
query: (body) => ({ url: 'login', method: 'POST', body }),
extraOptions: { maxRetries: 0 },
}),3Verify
Submit a request that returns an error; it must be sent once.
# browser devtools, Network tabConfirmed by redux.js.org
Cause 3 of 4
Errors that can never succeed are retried too
1Check
Retrying a rejected login or a missing resource only wastes time. A retryCondition applies to errors returned by baseQuery, and thrown errors are retried until maxRetries is reached.
grep -rn "retry.fail\|retryCondition" src2Fix
Wrap the base query so it calls retry.fail when the status shows a retry is pointless.
const baseQuery = retry(
async (args, api, extraOptions) => {
const result = await fetchBaseQuery({ baseUrl: '/api/' })(args, api, extraOptions)
if (result.error?.status === 401) {
retry.fail(result.error, result.meta)
}
return result
},
{ maxRetries: 5 },
)3Verify
Call an endpoint that returns 401; the error should appear after one request.
# browser devtools, Network tabConfirmed by redux.js.org
Cause 4 of 4
Refetching, not retrying, is sending the request
1Check
refetchOnFocus and refetchOnReconnect default to false and need setupListeners, so they only run if your code turned them on. refetchOnMountOrArgChange also defaults to false.
grep -rn "refetchOn\|setupListeners" src2Fix
Remove the option or set it back to false in createApi or on the hook.
export const api = createApi({
baseQuery,
refetchOnFocus: false,
refetchOnReconnect: false,
endpoints: () => ({}),
})3Verify
Switch tabs and back; no new request should appear.
# browser devtools, Network tabConfirmed by redux.js.org
How do I stop it coming back?
- Decide per endpoint whether a retry is safe; mutations that create things usually should not repeat.
- Treat 4xx responses as final with retry.fail.
- Keep one place where baseQuery is built so the retry setting is easy to find.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix