I noticed that some fetch requests don't reach the server when I change window.location just after sending the request (location change is not inside the promise response handler).
I tested on Chrome browser
# util.js
function sendLog(payload) {
const urlApi = `http://api.example.com/public/send-message`;
return fetch(urlApi, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
}).then(response => {
return response.json();
});
}
# main.js
sendLog({
project: 'FrontEnd-Card',
countryId: country
}).then(res => {
console.log(res);
});
window.location.replace(REDIRECT_URL);
I noticed this pattern: If network is fast, the log is successfully sent to the server If network is slow, the log doesn't reach the server.
Somehow redirection kills the outgoing request. Is this true ? Where can I find a detailed documentation about this behavior ?