-
Notifications
You must be signed in to change notification settings - Fork 32
feat: support for sendBeacon requests #651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
3d927d9
579265d
0fd45a3
2961b5c
23bc320
c734545
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,12 @@ var interceptor = { | |
window.sessionStorage.removeItem(NAMESPACE); | ||
} | ||
|
||
if (typeof window.navigator.sendBeacon == 'function') { | ||
replaceSendBeacon(); | ||
} else { | ||
console.error(PKG_PREFIX + 'sendBeacon API preconditions not met!'); | ||
} | ||
|
||
if (typeof window.fetch == 'function') { | ||
replaceFetch(); | ||
if ( | ||
|
@@ -54,6 +60,48 @@ var interceptor = { | |
|
||
done(window[NAMESPACE]); | ||
|
||
function replaceSendBeacon() { | ||
var _sendBeacon = window.navigator.sendBeacon; | ||
var interceptSendBeacon = function (url, payload) { | ||
var request = { | ||
method: 'POST', | ||
requestHeaders: {}, | ||
requestBody: payload, | ||
url: url, | ||
}; | ||
|
||
addPendingRequest(request); | ||
|
||
var isRequestQueuedForTransfer = _sendBeacon.apply(window.navigator, [ | ||
url, | ||
payload, | ||
]); | ||
|
||
completeSendBeaconRequest(request, { | ||
body: isRequestQueuedForTransfer, | ||
}); | ||
|
||
// Forward the original response to the application on the current tick. | ||
return isRequestQueuedForTransfer; | ||
}; | ||
|
||
window.navigator.sendBeacon = function (target, data) { | ||
var url = target; | ||
|
||
if (target instanceof URL) { | ||
url = target.href; | ||
} | ||
|
||
if (data instanceof Blob) { | ||
return data.text().then(function (payload) { | ||
return interceptSendBeacon(url, payload); | ||
}); | ||
} | ||
|
||
|
||
return interceptSendBeacon(url, data); | ||
}; | ||
} | ||
|
||
function replaceFetch() { | ||
var _fetch = window.fetch; | ||
window.fetch = function () { | ||
|
@@ -98,16 +146,16 @@ var interceptor = { | |
|
||
// After decoding the request's body (which may have come from Request#text()) | ||
// and the response body, we can store the completed request. | ||
Promise.all([request.requestBody, responsePromise]).then(function ( | ||
results | ||
) { | ||
completeFetchRequest(request, { | ||
requestBody: results[0], | ||
body: results[1], | ||
statusCode: clonedResponse.status, | ||
headers: parseHeaders(clonedResponse.headers), | ||
}); | ||
}); | ||
Promise.all([request.requestBody, responsePromise]).then( | ||
tehhowch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function (results) { | ||
completeFetchRequest(request, { | ||
requestBody: results[0], | ||
body: results[1], | ||
statusCode: clonedResponse.status, | ||
headers: parseHeaders(clonedResponse.headers), | ||
}); | ||
}, | ||
); | ||
|
||
// Forward the original response to the application on the current tick. | ||
return response; | ||
|
@@ -240,6 +288,13 @@ var interceptor = { | |
pushToSessionStorage(startedRequest); | ||
} | ||
|
||
function completeSendBeaconRequest(startedRequest, completedRequest) { | ||
startedRequest.body = completedRequest.body; | ||
startedRequest.statusCode = completedRequest.body === true ? 200 : 500; | ||
startedRequest.__fulfilled = Date.now(); | ||
replaceInSessionStorage(startedRequest); | ||
} | ||
|
||
function completeFetchRequest(startedRequest, completedRequest) { | ||
// Merge the completed data with the started request. | ||
startedRequest.requestBody = completedRequest.requestBody; | ||
|
@@ -266,7 +321,7 @@ var interceptor = { | |
return JSON.parse(rawData); | ||
} catch (e) { | ||
throw new Error( | ||
PKG_PREFIX + 'Could not parse sessionStorage data: ' + e.message | ||
PKG_PREFIX + 'Could not parse sessionStorage data: ' + e.message, | ||
tehhowch marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
); | ||
} | ||
} | ||
|
@@ -355,7 +410,7 @@ var interceptor = { | |
parsed = JSON.parse(rawData); | ||
} catch (e) { | ||
throw new Error( | ||
PKG_PREFIX + 'Could not parse sessionStorage data: ' + e.message | ||
PKG_PREFIX + 'Could not parse sessionStorage data: ' + e.message, | ||
tehhowch marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title>sendBeacon request</title> | ||
</head> | ||
<body> | ||
<p id="text">This file makes a sendBeacon request to /post.json</p> | ||
<button id="buttonstring">Press me for string data</button> | ||
<button id="buttonjson">Press me for JSON data as Blob</button> | ||
<div id="response"></div> | ||
<script> | ||
'use strict'; | ||
|
||
(function (window, document) { | ||
var buttonstring = document.querySelector('#buttonstring'); | ||
var buttonjson = document.querySelector('#buttonjson'); | ||
|
||
buttonstring.addEventListener('click', function (evt) { | ||
var data = 'bar'; | ||
var isQueuedForDelivery = window.navigator.sendBeacon(new URL('/post.json', window.location.origin), data); | ||
|
||
if(isQueuedForDelivery) { | ||
document.querySelector('#response').textContent += "queued string for delivery\n"; | ||
} | ||
}); | ||
|
||
buttonjson.addEventListener('click', function (evt) { | ||
var data = new Blob([JSON.stringify({ foo: 'bar' })], { type: 'text/plain' }); | ||
var isQueuedForDelivery = window.navigator.sendBeacon('/post.json', data); | ||
|
||
if(isQueuedForDelivery) { | ||
document.querySelector('#response').textContent += "queued json for delivery\n"; | ||
} | ||
}); | ||
|
||
})(window, window.document); | ||
</script> | ||
</body> | ||
</html> |
Uh oh!
There was an error while loading. Please reload this page.