Navigator.sendBeacon() data size limits
我想使用全新的信标api。 我在网上搜索,但找不到数据,发送数据的大小限制是多少。
在参考文献中,它是为少量数据而写的,但是我必须知道很少...
如果设置了最大大小,则取决于用户代理(浏览器或其他)。
参见http://www.w3.org/TR/beacon/#sec-sendBeacon-method
您可以通过创建可变长度
例如,我使用此方法来确认Windows 7上的Chrome 40的限制为
示例代码(不对
1 2 3 4 5 6 7 8 9 10 | 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'); } |
在评论中回答Zbun的问题:
简而言之,
正如没有必要的答案中提到的那样,W3C规范指出:
The user agent MUST initiate a fetch with keepalive flag set, which restricts the amount of data that can be queued by such requests to ensure that beacon requests are able to complete quickly and in a timely manner.
这仍然留给每个实现实际的字节大小限制,目前大多数浏览器通常为64KB。
同样在相关的GitHub问题中,通过Fetch API#39强制有效载荷限制,提到传输中有效载荷的字节大小是计数的,并且具有类似的限制。您可以查看Chromium的测试文件,以了解64KB是Chromium的限制,并且与您指出的行为完全相同(超出限制后的后续请求)是合适的。
https://chromium.googlesource.com/chromium/src/+/c1a211aa583ade023aca652e9493c01603623aa3/third_party/WebKit/LayoutTests/http/tests/sendbeacon/beacon-allowance-limit.html