storage icon indicating copy to clipboard operation
storage copied to clipboard

Storage download (storage.from.download url) is cached in the CDN even for deleted file

Open GaryAustin1 opened this issue 2 years ago • 6 comments

Bug report

It is desirable to cache image and some file urls. The storage download function though seems more like a database call to get the file data from the database/storage system. A deleted file from storage (no object and data) will still download when called with supabase.storage download. Cache busting with ?bust=123 in the path provided to download does return 404. The download url does not appear to be cached in the browser, using a 2nd browser will still return a file even though deleted and never used on the browser. After 1 hour the download function does get a 404 error.

To Reproduce

Create a file in the UI in a private bucket. Download it with:

  .storage
  .from('avatars')
  .download('folder/avatar1.png')

Delete the file. Run the download code on any browser and file still downloads.

Expected behavior

This behavior should at least be documented as it is not clear download is cached by it appears just the CDN. Or download should not be cached at all as it is more like a database call then a url for an image.

Screenshots

System information

Latest Supabase instance and supabase.js code.

Additional context

GaryAustin1 avatar Mar 02 '22 00:03 GaryAustin1

Hi,

we face the same issue but in a different use case. We store expense tickets under the user folder being the name of the file the id of the expense. If for any reason the ticket gets replaced with a new image, the name of the file will be exactly the same expense_id.jpeg, so due to that cache of Supabase, when trying to access the preview of the ticke through the storage.from().download() method, it will still show the previous one.

Is there any workaround for this in the meantime it gets fixed?

Thanks

mousindev avatar Apr 07 '22 07:04 mousindev

You need to do the cache busting string added at end with time. ?bust=95857573738 for now.

GaryAustin1 avatar Apr 07 '22 14:04 GaryAustin1

Hi @GaryAustin1, thanks for the answer. So, if I understand correctly, you mean to append it to the path like below being the number the current timestamp?

this.supabase.storage
.from('avatars')
.download('folder/avatar1.png?bust=current_timestamp')

mousindev avatar Apr 07 '22 16:04 mousindev

That should work, pretty sure based on my submit that is what I did. The browser and CDN will think it is a new file. I would only use it though when you know you need immediate feedback as if you do that everywhere you will never cache images, etc.

GaryAustin1 avatar Apr 07 '22 16:04 GaryAustin1

Thanks @GaryAustin1. I just did the changes and seems to work. In this specific scenario, it is ok for me to not cache it as it is very unusual to replace a scan. Thanks!

mousindev avatar Apr 07 '22 19:04 mousindev

Well, In that case you can store something as update_time under your Database, (as we prefer to do, when we update anything...right !), and use that as a reference under any image url (let say <Image_url>?t=<update_time>), that will reduce a lot many hits, and also able to use cached file

rahul3v avatar Apr 12 '22 08:04 rahul3v

Closing as the current solution is to use a query string parameter to bust the cache. We'll make more improvements and automating the cache busting in the near future #120

fenos avatar Sep 28 '22 13:09 fenos