Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigator.sendBeacon() data size limits

I want to use the quite new beacon api. I searched the web but I couldn't find data what is the size limit of the sent data. In the reference it's written it's meant for small amount of data but I have to know how little...

like image 865
Zbun Avatar asked Mar 11 '15 14:03

Zbun


1 Answers

The maximum size, if set, will be up to the user agent (browser, or whatever).

See http://www.w3.org/TR/beacon/#sec-sendBeacon-method

You could easily create a test for the data size limit in your web page by creating strings of variable length N (starting with an arbitrarily high value for N, and using binary search), and check the return value of sendBeacon (per the spec, sendBeacon returns false when the user agent data limit is exceeded).

For example, I used this method to confirm that in Chrome 40 on Windows 7 the limit is 65536 (2^16).

Example code (without the binary search for n):

var url = 'http://jsfiddle.net?sendbeacon';
var n = 65536; // sendBeacon limit for Chrome v40 on Windows (2^16)

// this method courtesy of http://stackoverflow.com/questions/14343844/create-a-string-of-variable-length-filled-with-a-repeated-character
var data = new Array(n+1).join('X'); // generate string of length n

if(!navigator.sendBeacon(url, data))
{
   alert('data limit reached');
}
like image 139
nothingisnecessary Avatar answered Sep 21 '22 19:09

nothingisnecessary