How to specify encode file formats?

Hi, there. I’m using Caddy v2.0.0 and I’d like to specify encode formats to be compressed. However, I can’t figure them out by reading the documentation page (encode (Caddyfile directive) — Caddy Documentation) which says " <formats…> is the list of encoding formats to enable." Ok, but the devil is in the details.

What is this list of formats?

  1. json js css html
  2. *.json, *.js, *.css, *.html
  3. application/json text/html text/css text/javascript

Can anybody give me a complete example using those extensions in an encode directive?
I wish there were more examples in the documentation page.

The supported formats are right there on the page. zstd and gzip. The brotli format was removed from core, the docs will be updated soon to reflect that though.

You can use the encode directive along with any request matcher to match requests for whatever types of files you need:

1 Like

Ah… It is an encoding list. My bad. But even then, I couldn’t make it work. This is my Caddyfile:

localhost {

    @encode_exts {
        path_regexp \.(html|js|css|svg)$
    }

    encode @encode_exts gzip zstd

    file_server {
        root /dist
    }
}

And this is the response I get:

$ http --verify no https://localhost
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 2511
Content-Type: text/html; charset=utf-8
Date: Fri, 15 May 2020 16:00:33 GMT
Etag: "qadqxx1xr"
Last-Modified: Fri, 15 May 2020 15:56:21 GMT
Server: Caddy

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="main.css" />
        <title>Hello World</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Illum
            impedit, nam, consequatur sequi sapiente reiciendis maxime sint
            explicabo voluptates expedita, reprehenderit laboriosam rerum
            asperiores velit pariatur. Voluptatem nisi odit qui!
        </p>
        <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Illum
            impedit, nam, consequatur sequi sapiente reiciendis maxime sint
            explicabo voluptates expedita, reprehenderit laboriosam rerum
            asperiores velit pariatur. Voluptatem nisi odit qui!
        </p>
        <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Illum
            impedit, nam, consequatur sequi sapiente reiciendis maxime sint
            explicabo voluptates expedita, reprehenderit laboriosam rerum
            asperiores velit pariatur. Voluptatem nisi odit qui!
        </p>
        <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Illum
            impedit, nam, consequatur sequi sapiente reiciendis maxime sint
            explicabo voluptates expedita, reprehenderit laboriosam rerum
            asperiores velit pariatur. Voluptatem nisi odit qui!
        </p>
        <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Illum
            impedit, nam, consequatur sequi sapiente reiciendis maxime sint
            explicabo voluptates expedita, reprehenderit laboriosam rerum
            asperiores velit pariatur. Voluptatem nisi odit qui!
        </p>
        <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Illum
            impedit, nam, consequatur sequi sapiente reiciendis maxime sint
            explicabo voluptates expedita, reprehenderit laboriosam rerum
            asperiores velit pariatur. Voluptatem nisi odit qui!
        </p>
        <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Illum
            impedit, nam, consequatur sequi sapiente reiciendis maxime sint
            explicabo voluptates expedita, reprehenderit laboriosam rerum
            asperiores velit pariatur. Voluptatem nisi odit qui!
        </p>
    </body>
</html>

What am I doing wrong?

I would suggest a matcher more like:

@encode_exts {
    path *.html *.js *.css *.svg
}

You’re matching on paths with .html, but your request doesn’t have .html!

I’m not sure what the best way to handle that would be. Maybe add / as a path to the matcher as well, to handle index.html

:+1: to @matt’s suggestion of the path matcher instead of path_regexp, it’ll be marginally faster, and it’s easier to read I think.

With / added:

@encode_exts {
   path / *.html *.js *.css *.svg
}
2 Likes

Thank you all!

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.