FileSaver.js icon indicating copy to clipboard operation
FileSaver.js copied to clipboard

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

Open mrdulin opened this issue 1 year ago • 3 comments

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)?

mrdulin avatar Jul 12 '24 08:07 mrdulin

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.

dongyuwei avatar Jul 16 '24 03:07 dongyuwei

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?

mrdulin avatar Jul 17 '24 02:07 mrdulin

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.

dongyuwei avatar Jul 18 '24 00:07 dongyuwei

It's completely arbitrary. This is an unfortunate side effect of the web platform not having 'download completed' events

eligrey avatar Aug 14 '25 02:08 eligrey