flet icon indicating copy to clipboard operation
flet copied to clipboard

bug - flet caches local image loads

Open hololeo opened this issue 1 year ago • 11 comments

With image container dev can set the src property to load in a new image. This image can be remote or local . I have found with local loads of pngs, Flet go server will not send back latest file. I discovered this writing a tool that converts svg code to a png file and saving it locally everytime the svg code changed a new png is generated but saved with same file name (output.png) When flet code tries to image.src = output.png the same image is returned.

you can view this behaviour in this code

https://github.com/hololeo/flet-hacks/blob/main/flet_svg_tool.py (change to below)

  png = Image (
    src = img_file_path
    #src_base64 = b64_string
  )

The work around is to convert the png to base64 then pass it to flet. But this produces a new side effect - the image flickers (probably a flutter bug)

I suspect this is happening due to caching in fletd. perhaps in the http headers

https://imagekit.io/blog/ultimate-guide-to-http-caching-for-static-assets/

hololeo avatar Sep 16 '22 21:09 hololeo

...but, have you tried appending something to an image URL on Flet side, to force its reload, like:

Image(src=f"output.png?ts={time.time()}")

FeodorFitsner avatar Sep 18 '22 00:09 FeodorFitsner

Is it still actual?

FeodorFitsner avatar Sep 28 '22 18:09 FeodorFitsner


Image(src=f"output.png?ts={time.time()}")

What is the role of the ?ts= here please ?

ndonkoHenri avatar Sep 29 '22 10:09 ndonkoHenri

It could be anything with a random value. That's a usual trick on the web to make client believe it's a new resource.

FeodorFitsner avatar Sep 29 '22 15:09 FeodorFitsner

I see. Any Link or resource please? I want to learn more.

ndonkoHenri avatar Sep 29 '22 16:09 ndonkoHenri

Just googled: https://www.uptrends.com/support/kb/monitor-settings/random-url-value-cache-busting

FeodorFitsner avatar Sep 29 '22 18:09 FeodorFitsner

Thanks. 👍🏾

ndonkoHenri avatar Sep 30 '22 01:09 ndonkoHenri

Closing the issue? ;)

FeodorFitsner avatar Sep 30 '22 01:09 FeodorFitsner

i think this is still open as reported here:

Screen Shot 2022-10-02 at 7 02 58 AM

hololeo avatar Oct 02 '22 14:10 hololeo

The workaround suggested by @hololeo on Discord

another work around may be to use an http path to the local image and use 'cache buster' to the tail of that url string i think the fix is in setting a cache expire header on the go server (or something like that)

doesn't work. Perhaps I'm doing something wrong, but

img.src = "file:///Volumes/SSD/Data/Devel/proj1/assets/preview.png?{random.randint(0, 1000)}"

fails to load images at all. My browser loads the images above OK.

abulka avatar Oct 03 '22 00:10 abulka

instead of file:/// use https://wahteveryourhost:yourport that flet server launches with use the debug to see the address

import logging
logging.basicConfig(level=logging.DEBUG)

hololeo avatar Oct 03 '22 01:10 hololeo