Default Retry Behavior
By default, ofetch retries failed requests once for safe methods (GET, HEAD, OPTIONS, TRACE):Retries only occur when the response status code matches one of the retryable status codes. Network errors and timeouts are also retried.
Retry Status Codes
The following HTTP status codes trigger automatic retries:- 408 - Request Timeout
- 409 - Conflict
- 425 - Too Early (Experimental)
- 429 - Too Many Requests
- 500 - Internal Server Error
- 502 - Bad Gateway
- 503 - Service Unavailable
- 504 - Gateway Timeout
Configuring Retry Count
You can customize the number of retry attempts using theretry option:
number | false
default:"1 for safe methods, 0 for unsafe methods"
The number of times to retry the request. Set to
false to disable retries completely.Retry Delay
You can add a delay between retry attempts:number | ((context: FetchContext) => number)
default:"0"
Delay in milliseconds between retry attempts. Can be a fixed number or a function that returns the delay based on the fetch context.
Fixed Delay
Dynamic Delay (Exponential Backoff)
Retry with Jitter
Custom Retry Status Codes
You can override the default retry status codes:number[]
default:"[408, 409, 425, 429, 500, 502, 503, 504]"
Array of HTTP status codes that should trigger a retry.
Retry with Rate Limiting
Handle rate limiting with exponential backoff:Abort Signal and Retries
Abort signals prevent retries from happening:Retry Logic Flow
- Request is made
- If request fails or returns a retryable status code:
- Check if
retrycount is greater than 0 - Check if status code is in
retryStatusCodes(or default list) - Check if request was not aborted
- Check if
- If all checks pass:
- Wait for
retryDelaymilliseconds - Retry the request with
retrycount decremented by 1
- Wait for
- If retry count reaches 0, throw
FetchError