jsPsych experiment, token randomly consumed, no data stored

Thanks a lot! I think I have identified the problem, but I don’t know how to deal with it.
The data file which is generated has a size of around 2MB, so it can easily take 15 seconds to upload the data to the server.
I am redirecting the participant to Prolific at the end of the experiment. Due to that the upload result POST seems to stay pending and in the end is not finished at all.
Removing window.location.replace() to Prolific isn’t a problem, but the data is also not stored when the participant closes the browser tab directly after the experiment.

The solution maybe would be something like sending an event at the end of the upload to tell the participant that he can now close the experiment.
Do you see any other solutions?