1

I'm new in Rust and Rocket. I mount my static files in Rocket as follows:

.mount("/", StaticFiles::new("static", options))

It works good for images or styles but it's not quite what I expect from adding of scripts. I want to use compressed script files like *.js.gz instead of *.js. The HTML that links the script looks like:

<script type="text/javascript" src="/scripts/index.js.gz"></script>

How can I add headers to response in order to allow the use of compressed scripts? I assume I shouldn't use rocket_contrib::serve::StaticFiles for script files here. In ASP.NET Core I do this as follows:

app.UseStaticFiles(new StaticFileOptions
        {
            OnPrepareResponse = content =>
            {
                if (!content.File.Name.EndsWith(".js.gz")) return;
                content.Context.Response.Headers["Content-Type"] = "text/javascript";
                content.Context.Response.Headers["Content-Encoding"] = "gzip";
            }
        });

Is there the same way for Rust-Rocket? Thanks!

  • 1
    AFAICS there is currently no automagic way to do this in Rocket. `StaticFiles` uses [`ContentType::from_extension`](https://docs.rs/rocket/0.4.10/rocket/http/struct.ContentType.html#method.from_extension) under it's hood, which will set `Content-Type` to `application/gzip` with no `Content-Encoding`; which is not what you want. You'll have to implement [`response::Responder`](https://docs.rs/rocket/0.4.10/rocket/response/trait.Responder.html) manually. – user2722968 Jul 12 '21 at 17:13

0 Answers0