Use fetch with keepalive: true instead of navigator.sendBeacon

This allows customizing the headers of the request. It has the same
semantics as `navigator.sendBeacon` and it's the recommended alternative
to `sendBeacon` for customizing the request:

https://www.w3.org/TR/beacon/#introduction

This commit also fixes `settings.requestHandler` not being called when
using `settings.sendBeaconCommit`.
This commit is contained in:
Rubén Herrero
2022-02-16 11:37:26 +01:00
parent 3aec87829a
commit b5c21ea86f
13 changed files with 77 additions and 109 deletions

31
dist/aicc.js vendored
View File

@@ -1795,26 +1795,19 @@ var BaseAPI = /*#__PURE__*/function () {
}
} else {
try {
var headers = {
type: settings.commitRequestDataType
};
var blob;
if (params instanceof Array) {
blob = new Blob([params.join('&')], headers);
} else {
blob = new Blob([JSON.stringify(params)], headers);
}
params = settings.requestHandler(params);
fetch(url, {
method: 'POST',
body: params instanceof Array ? params.join('&') : JSON.stringify(params),
headers: _objectSpread(_objectSpread({}, settings.xhrHeaders), {}, {
'Content-Type': settings.commitRequestDataType
}),
credentials: settings.xhrWithCredentials ? 'include' : undefined,
keepalive: true
});
result = {};
if (navigator.sendBeacon(url, blob)) {
result.result = global_constants.SCORM_TRUE;
result.errorCode = 0;
} else {
result.result = global_constants.SCORM_FALSE;
result.errorCode = 101;
}
result.result = global_constants.SCORM_TRUE;
result.errorCode = 0;
} catch (e) {
console.error(e);
api.processListeners('CommitError');

2
dist/aicc.js.map vendored

File diff suppressed because one or more lines are too long

8
dist/aicc.min.js vendored

File diff suppressed because one or more lines are too long

31
dist/scorm-again.js vendored
View File

@@ -1795,26 +1795,19 @@ var BaseAPI = /*#__PURE__*/function () {
}
} else {
try {
var headers = {
type: settings.commitRequestDataType
};
var blob;
if (params instanceof Array) {
blob = new Blob([params.join('&')], headers);
} else {
blob = new Blob([JSON.stringify(params)], headers);
}
params = settings.requestHandler(params);
fetch(url, {
method: 'POST',
body: params instanceof Array ? params.join('&') : JSON.stringify(params),
headers: _objectSpread(_objectSpread({}, settings.xhrHeaders), {}, {
'Content-Type': settings.commitRequestDataType
}),
credentials: settings.xhrWithCredentials ? 'include' : undefined,
keepalive: true
});
result = {};
if (navigator.sendBeacon(url, blob)) {
result.result = global_constants.SCORM_TRUE;
result.errorCode = 0;
} else {
result.result = global_constants.SCORM_FALSE;
result.errorCode = 101;
}
result.result = global_constants.SCORM_TRUE;
result.errorCode = 0;
} catch (e) {
console.error(e);
api.processListeners('CommitError');

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

31
dist/scorm12.js vendored
View File

@@ -1670,26 +1670,19 @@ var BaseAPI = /*#__PURE__*/function () {
}
} else {
try {
var headers = {
type: settings.commitRequestDataType
};
var blob;
if (params instanceof Array) {
blob = new Blob([params.join('&')], headers);
} else {
blob = new Blob([JSON.stringify(params)], headers);
}
params = settings.requestHandler(params);
fetch(url, {
method: 'POST',
body: params instanceof Array ? params.join('&') : JSON.stringify(params),
headers: _objectSpread(_objectSpread({}, settings.xhrHeaders), {}, {
'Content-Type': settings.commitRequestDataType
}),
credentials: settings.xhrWithCredentials ? 'include' : undefined,
keepalive: true
});
result = {};
if (navigator.sendBeacon(url, blob)) {
result.result = global_constants.SCORM_TRUE;
result.errorCode = 0;
} else {
result.result = global_constants.SCORM_FALSE;
result.errorCode = 101;
}
result.result = global_constants.SCORM_TRUE;
result.errorCode = 0;
} catch (e) {
console.error(e);
api.processListeners('CommitError');

2
dist/scorm12.js.map vendored

File diff suppressed because one or more lines are too long

6
dist/scorm12.min.js vendored

File diff suppressed because one or more lines are too long

31
dist/scorm2004.js vendored
View File

@@ -1670,26 +1670,19 @@ var BaseAPI = /*#__PURE__*/function () {
}
} else {
try {
var headers = {
type: settings.commitRequestDataType
};
var blob;
if (params instanceof Array) {
blob = new Blob([params.join('&')], headers);
} else {
blob = new Blob([JSON.stringify(params)], headers);
}
params = settings.requestHandler(params);
fetch(url, {
method: 'POST',
body: params instanceof Array ? params.join('&') : JSON.stringify(params),
headers: _objectSpread(_objectSpread({}, settings.xhrHeaders), {}, {
'Content-Type': settings.commitRequestDataType
}),
credentials: settings.xhrWithCredentials ? 'include' : undefined,
keepalive: true
});
result = {};
if (navigator.sendBeacon(url, blob)) {
result.result = global_constants.SCORM_TRUE;
result.errorCode = 0;
} else {
result.result = global_constants.SCORM_FALSE;
result.errorCode = 101;
}
result.result = global_constants.SCORM_TRUE;
result.errorCode = 0;
} catch (e) {
console.error(e);
api.processListeners('CommitError');

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1150,24 +1150,20 @@ export default class BaseAPI {
}
} else {
try {
const headers = {
type: settings.commitRequestDataType,
};
let blob;
if (params instanceof Array) {
blob = new Blob([params.join('&')], headers);
} else {
blob = new Blob([JSON.stringify(params)], headers);
}
params = settings.requestHandler(params);
fetch(url, {
method: 'POST',
body: params instanceof Array ? params.join('&') : JSON.stringify(params),
headers: {
...settings.xhrHeaders,
'Content-Type': settings.commitRequestDataType,
},
credentials: settings.xhrWithCredentials ? 'include' : undefined,
keepalive: true,
});
result = {};
if (navigator.sendBeacon(url, blob)) {
result.result = global_constants.SCORM_TRUE;
result.errorCode = 0;
} else {
result.result = global_constants.SCORM_FALSE;
result.errorCode = 101;
}
result.result = global_constants.SCORM_TRUE;
result.errorCode = 0;
} catch (e) {
console.error(e);
api.processListeners('CommitError');