diff --git a/FormData.js b/FormData.js index 656d745..9a49f86 100644 --- a/FormData.js +++ b/FormData.js @@ -12,6 +12,7 @@ if (typeof Blob === 'function' && (typeof FormData === 'undefined' || !FormData. // To be monkey patched const _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send const _fetch = global.Request && global.fetch + const _sendBeacon = global.navigator && global.navigator.sendBeacon // Unable to patch Request constructor correctly // const _Request = global.Request @@ -391,5 +392,15 @@ if (typeof Blob === 'function' && (typeof FormData === 'undefined' || !FormData. } } + // Patch navigator.sendBeacon to use native FormData + if (_sendBeacon) { + global.navigator.sendBeacon = function (url, data) { + if (data instanceof FormDataPolyfill) { + data = data['_asNative']() + } + return _sendBeacon.call(global.navigator, url, data) + } + } + global['FormData'] = FormDataPolyfill } diff --git a/README.md b/README.md index f61872d..744946b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ A `FormData` polyfill This polyfill conditionally replaces the native implementation rather then fixing the missing functions, since otherwise there is no way to get or delete existing values in the FormData object. -Therefore this also patches `XMLHttpRequest.prototype.send` and `fetch` to send the FormData as a blob. +Therefore this also patches `XMLHttpRequest.prototype.send` and `fetch` to send the `FormData` as a blob, +and `navigator.sendBeacon` to send native `FormData`. I was unable to patch the Response/Request constructor so if you are constructing them with FormData you need to call `fd._blob()` manually.