I have an express nodejs server which does some calculations and creates a pdf file and sends it as the response. the problem is, when the user tries to download the file, the endpoint is called twice by the browser so the calculations are done twice and it will take twice as long and it will take around 20 seconds for the request to be answered . i checked and saw that the endpoint itself is called twice not favicon.ico or other things.
the file is not saved on the server and is created as buffer and then piped to the client. and the endpoint is not called by the frontend code or fetched by something, it is just opened in a new tab in the browser. some of the codes are listed below:
let buffer = await createPdfBuffer() // a custom function that creates the pdf as buffer
const readStream = new stream.PassThrough();
readStream.end(buffer);
res.set('Content-disposition', 'attachment; filename=' + file.pdf);
res.set('Content-Type', 'application/pdf');
readStream.pipe(res);