swissspidy / media-experiments

WordPress media experiments
GNU General Public License v2.0
69 stars 1 forks source link

Improve blob URL handling #517

Closed swissspidy closed 2 weeks ago

swissspidy commented 3 weeks ago

Blob URLs are currently created in lots of places, but not explicitly recoked again with revokeObjectURL. Ideally that should happen as soon as possible, but only when s blob URL isn‘t used anywhere anymore. So the safest time to do it would be after removing an item from the queue.

Need to keep track of all blob URLs per item so they can be revoked upon removal.

swissspidy commented 3 weeks ago

From the MDN documentation:

Each time you call createObjectURL(), a new object URL is created, even if you've already created one for the same object. Each of these must be released by calling URL.revokeObjectURL() when you no longer need them.

Browsers will release object URLs automatically when the document is unloaded; however, for optimal performance and memory usage, if there are safe times when you can explicitly unload them, you should do so.

There are two more places outside the upload queue where object URLs are created:

Gutenberg handles the revoking in the uploadMedia callback:

https://github.com/WordPress/gutenberg/blob/3bb138ed859682e9d9567b8e02278e2bd58b0b2f/packages/media-utils/src/utils/upload-media.js#L69-L73

https://github.com/WordPress/gutenberg/blob/3bb138ed859682e9d9567b8e02278e2bd58b0b2f/packages/media-utils/src/utils/upload-media.js#L176-L192

swissspidy commented 3 weeks ago

Tangent:

Blob URLs are not supported in service workers (possibly needed for #245).

Need to refactor getDominantColor() to accept a buffer instead of a (blob) URL. Ditto for vipsHasTransparency and getBlurHash

Blob URLs are also used for getPosterFromVideo and getImageFromPdf, but those are not necessarily relevant for #245.

swissspidy commented 2 weeks ago

chrome://blob-internals/ could be used for debugging