Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Ajax

Ajax: Fix a Retry Loop Re-sending a Request After a 304 Response

- 26.11.26 - ErcanOPAK

🔄 Ajax: Fix a Retry Loop Re-sending a Request After a 304 Response

A 304 Not Modified response is the browser’s HTTP cache telling the server “I already have this, don’t resend it” – and critically, a 304 has no body at all, since the whole point is to avoid retransmitting data the client already has cached. A retry wrapper written to treat “response has no usable body” or “JSON.parse failed on an empty string” as a failed request will treat a perfectly successful 304 exactly like a network error, and dutifully retry a request that didn’t need to be sent again in the first place – sometimes in a loop, since the retried request gets cached-and-304’d again every time.

🔎 The Problem

async function fetchWithRetry(url, attempts = 3) {
  const res = await fetch(url);
  const text = await res.text();
  if (!text) {                 // true for a 304's empty body
    if (attempts > 0) return fetchWithRetry(url, attempts - 1);
    throw new Error('Empty response');
  }
  return JSON.parse(text);
}
// A valid 304 keeps triggering retries, burning all 3 attempts
// and still throwing, even though the resource is unchanged and fine

âś… The Fix

  • Check res.status === 304 explicitly before inspecting the body, and read the cached value back from the browser’s own cache API or your own local copy instead of expecting the network response to carry it.
  • Treat “empty body” and “request failed” as separate conditions in the retry logic – gate retries on res.ok being false or a thrown network error, not on whatever the body happens to contain.
  • If you’re fetching JSON through a wrapper that needs a body every time, set cache: ‘no-store’ (or an appropriate Cache-Control header) deliberately where a fresh body is actually required, rather than fighting the browser’s caching behavior from inside the retry logic.

🚨 Why This Is Dangerous

A retry loop that keeps firing against an endpoint that’s already answering correctly adds real, invisible load – from the server’s perspective every attempt looks like a legitimate new request, not an error being quietly chased by a client that misunderstood a 304.

A 304 isn’t a failure to retry – it’s the server confirming you already have the right answer.

— Frontend Engineer

Related posts:

AJAX “POST Suddenly Fails” — The Missing Content-Type Header

AJAX “POST Suddenly Fails” — The Missing Content-Type Header

AJAX Requests Timeout Randomly — Browser Connection Limits

AJAX Requests Timeout Randomly — Browser Connection Limits

AJAX: Build Real-time Apps with WebSockets

AJAX: Build Real-time Apps with WebSockets

Views: 10
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Git: Fix a Sparse-Checkout Silently Excluding a New Subfolder
Git: Fix a Shallow Clone Rejected by a Branch Protection Rule

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com