Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

RTK Query keeps repeating a request that fails

ErrorThe same failed request appears about six times in the network tab

Short 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.

  • Applies to Redux Toolkit 2, React 19, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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=*.js
  2. 2Match what you see, then jump to its fix

    1. 1One failing call causes the first request plus five more, spaced further apart each timebaseQuery is wrapped with retry and uses the default countFix
    2. 2Only a mutation or a single query should fail fast; the rest may retryOne endpoint should never retryFix
    3. 3401, 403 or 404 responses are repeated before the error is shownErrors that can never succeed are retried tooFix
    4. 4Requests repeat only after you refocus the tab or reconnect to the networkRefetching, not retrying, is sending the requestFix

What does the error look like?

  • The same failed request appears about six times in the network tab
  • The 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(" src

2Fix

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 URL

Confirmed by redux.js.org

Did this fix it?

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" src

2Fix

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 tab

Confirmed by redux.js.org

Did this fix it?

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" src

2Fix

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 tab

Confirmed by redux.js.org

Did this fix it?

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" src

2Fix

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 tab

Confirmed by redux.js.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.