Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Allow specifying a cache-control header for serving static files. #2264

Closed
wants to merge 1 commit into from
Closed

Draft: Allow specifying a cache-control header for serving static files. #2264

wants to merge 1 commit into from

Conversation

dermesser
Copy link

Hi, first-time contributor here! I'd like to request some comments on this change before finalizing it and writing tests.

The idea is that when serving e.g. Javascript or image assets through Rocket, I'd like clients not to request the whole resource every time. I haven't found an established way of doing this in Rocket (except for serving static resources from a different webserver).

It's unclear to me if my solution is too hacky, but I've also not found a better place to implement this in. Maybe you can point me to it if there is.

@Zusier
Copy link

Zusier commented Jul 15, 2022

Not sure if this is useful here or not but, I currently use this to allow any type that implements a Responder trait to have a cache-control header:

use rocket::response;

pub struct CacheableResponse<T> {
    pub data: T,
    pub cache_control: String,
}

impl<'r, 'o: 'r, T: response::Responder<'r, 'o>> response::Responder<'r, 'o> for CacheableResponse<T> {
    fn respond_to(self, req: &'r rocket::Request<'_>) -> response::Result<'o> {
        response::Response::build_from(self.data.respond_to(req)?)
            .raw_header("Cache-Control", self.cache_control)
            .ok()
    }
}

@dermesser
Copy link
Author

@Zusier thank you, this is my "backup plan", so to say :) I proposed adding this to the file serving code as it is likely a frequent need but I'm ok if your way is the "official" way going forward. (in that case I may file a PR for the documentation later :)

@JockeTF
Copy link
Contributor

JockeTF commented Jul 16, 2022

I wanted to bundle my assets into a single binary, so I did something slightly different. Perhaps that's a bit too specific to be of any help help in this case though.

@SergioBenitez
Copy link
Member

Please see #95 (comment).

@dermesser dermesser deleted the fileserver-cachecontrol branch April 15, 2023 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants