SinTan1729/chhoto-url

Add cache control headers

Closed this issue ยท 9 comments

I love this project! ๐Ÿ™

Today I was struggling to figure out why some updated images I put in /resources/assets were still loading the old versions. I run Chhoto behind Cloudflare proxy, and apparently it caches png and other static assets by default if the webserver doesn't set the Cache-Control header to private, no-store, no-cache, or max-age=0.

I see in the docs at https://docs.rs/actix-web/latest/actix_web/http/header/struct.CacheControl.html that there seems to be a fairly easy way to add these cache control headers. So that's what this request is about!

I am not sure if I am up to a full PR but if that is required I can try.

Hi @luckman212, I think adding these header to the assets will be a good idea. I'll probably add it as a configurable setting, enabled by default.

It's easy to add these to anything that implements Responder. But the assets are served using a actix_files::Files struct. After a quick glance at the documentation, I couldn't find a way to add these headers to it.

Ah I see. I'm not familiar with Actix or Rust much but seems like they have a plugin system called Middleware that allows for this.

example:

https://git.itsufficient.me/rust/actix-cachecontrol-middleware

Can you test if the latest commit is working? The latest dev build can be accessed using the dev image tag.

Thanks @SinTan1729 - I pulled from the dev tag and tested it but I don't think it's working. I don't see any cache-control directives in the header response:

image

I'm a little confused. I am getting the correct response header.
image
You're testing locally, right? Maybe try force refreshing the page to make sure the browser isn't using cached responses from earlier?

Hmm I was testing from Chrome and tried shift+refresh which at least USED to force a cache miss but not sure it does any more...

I tested in Safari just now, which I don't use and so should have a clean cache, and went directly to the docker container, bypassing Cloudflare proxy, and you're right I do see the header there. So, looks like it's working. Maybe I need to wait 24h for the existing cached content to flush out. Thanks for all this help. Seems to be working!

image

Happy to help. Now I just need to do some performance testing to figure out if I should keep the cache disabled by default, since no one would be changing the resources all that often in practice.

Quick update: I changed it a bit. Now it sends no headers by default. To achieve what you want, pass cache_control_header=no-cache, private to the docker image.

This is great, thank you again! ๐Ÿ™