eligrey / FileSaver.js

An HTML5 saveAs() FileSaver implementation
https://eligrey.com/blog/saving-generated-files-on-the-client-side/
Other
21.55k stars 4.38k forks source link

Why is there a 40-second delay in calling the `URL.revokeObjectURL` method? #811

Open mrdulin opened 2 months ago

mrdulin commented 2 months ago

I asked same question on Stack Overflow, see here.

The source code FileSaver.js#L106

a.href = URL.createObjectURL(blob)
setTimeout(function () { URL.revokeObjectURL(a.href) }, 4E4) // 40s
setTimeout(function () { click(a) }, 0)

Why is there a 40-second delay in calling the URL.revokeObjectURL method? Why don't call it immediately after calling URL.createObjectURL(blob)?

dongyuwei commented 2 months ago

https://developer.mozilla.org/en-US/docs/Web/API/URL/revokeObjectURL_static

The revokeObjectURL() static method of the URL interface releases an existing object URL which was previously created by calling URL.createObjectURL().

Call this method when you've finished using an object URL to let the browser know not to keep the reference to the file any longer.

So the 40-second delay is just to relase the resource, prevent any memory leak.

mrdulin commented 2 months ago

https://developer.mozilla.org/en-US/docs/Web/API/URL/revokeObjectURL_static

The revokeObjectURL() static method of the URL interface releases an existing object URL which was previously created by calling URL.createObjectURL().

Call this method when you've finished using an object URL to let the browser know not to keep the reference to the file any longer.

So the 40-second delay is just to relase the resource, prevent any memory leak.

Why 40 seconds rather than 30 seconds or 60 seconds?

dongyuwei commented 2 months ago

Maybe 40 seconds or 30 seconds or 60 seconds are all okay, seems to don't matter. I guess it's just used to wait and ensure the file is downloaded completely.