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 Fetch Keepalive Flag Truncating a Large Payload

- 29.11.26 - ErcanOPAK

🔄 Fix a Fetch Keepalive Flag Truncating a Large Payload

Setting keepalive: true on a fetch() call is the standard way to make a request survive the page unloading — useful for a final analytics ping or a last save on navigation away — but browsers cap the total size of all in-flight keepalive request bodies combined, typically around 64 KB in Chromium. A request built to carry a larger payload (a batch of queued events, a full form snapshot) doesn’t get a clear error when it exceeds that cap; depending on the browser, it either fails to send at all or the request is rejected before it leaves the page, with nothing in your .catch() because the fetch promise itself may never even reject in the way you’d expect from a normal network failure.

🔎 The Problem

window.addEventListener("pagehide", () => {
  fetch("/api/analytics/batch", {
    method: "POST",
    keepalive: true,
    body: JSON.stringify(queuedEvents), // 180 KB of queued events
  });
  // Exceeds the browser's combined keepalive body size limit (~64KB in
  // Chromium) - request silently fails to be queued, queuedEvents is lost,
  // and no catch block fires because the failure happens before the
  // fetch's own promise machinery gets involved

✅ Fix: A Fetch Keepalive Flag Truncating a Large Payload

  • Check the serialized payload size before sending and chunk it into multiple requests under the browser’s keepalive limit, rather than sending one large batch on unload.
  • For the common final-ping-on-unload case, prefer navigator.sendBeacon() instead, and check its own boolean return value (it also has a size cap, but tells you synchronously whether the browser accepted the request).
  • Flush and send analytics data incrementally as it’s generated rather than queuing everything for a single request at unload, so there’s nothing large left to send in that narrow, failure-prone window.

🚨 Why This Is Dangerous

  • A payload that silently fails to send on page unload leaves no trace in your client-side logs, since the page is already gone by the time anyone could see an error — the first sign is just missing data on the server side.

keepalive buys a request a little extra time after the page is gone — it was never meant to buy it unlimited size.

— Frontend Engineer

Related posts:

Send JSON the Right Way

Send JSON the Right Way

Ajax: Understand CORS for Cross-Origin Requests

Ajax: Understand CORS for Cross-Origin Requests

Ajax Requests Succeed but Data Is Stale

Ajax Requests Succeed but Data Is Stale

Views: 5
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

WordPress: Fix Media Library Thumbnails Breaking After a CDN Switch
SQL Server: Fix a Scalar UDF Inlining Silently Changing a Result

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