I’m working on React app that gets some urls from server in format
{
filename: 'filename',
url: 'url',
},
and then renders buttons in very common way
<button onClick={() => downloadFile(file.url, file.filename)}>{file.filename}</button>
where downloadFile is a simple function
export const downloadFile = (url: string, filename: string) => {
const link = document.createElement('a');
link.href = `${url}?download`;
link.download = `${filename}`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
So normally when user clicks on button, browser starts downloading file.
Problem is when you spend about 5 minutest on page without refreshing, and then click on button, Chrome shows error: "Check internet connection"
After refreshing page/url change everything works fine again
And the problem is not appearing in Mozila
2
Answers
I updated the downloadFile function so it now fetches the latest download URL from the server each time you click to download, because there was an issue where users got a "Check internet connection" error if they waited too long. The function now uses a try-catch block to handle errors better: it fetches the current URL and filename based on the fileId, then creates a temporary element to start the download. If it fails, it logs the error and shows an alert asking the user to check their internet connection. This change makes sure the URL is always fresh and improves the user experience when something goes wrong.