Caddyserver v2 plugin that minifies HTML, CSS, JavaScript and SVG responses on the fly. It uses tdewolff's library so, let's thank him!.
Tested with Caddy v2.11.9 (requires Go 1.26+ to build).
xcaddy build v2.11.9 --with github.com/devetek/caddyserver-minifierResponses with one of the following Content-Type are buffered, minified in a single pass and sent with an updated Content-Length:
| Type | Content-Type |
|---|---|
| html | text/html |
| css | text/css |
| js | application/javascript, text/javascript, application/x-javascript, ... |
| svg | image/svg+xml |
Inline <style>, <script> and <svg> inside HTML are minified as well, using the CSS/JS/SVG configuration.
Responses are passed through untouched when they are:
- not
2xx, or are204/206responses, or aHEADrequest - already compressed (
Content-Encodingis set, e.g. a gzipped upstream behindreverse_proxy) - marked with
Cache-Control: no-transform
If minification fails the original body is served and a warning is logged. A strong ETag is converted to a weak one since the minified body is no longer byte-identical.
Because this directive does not come standard with Caddy, you may use route to order it the way you want. Place encode before minifier so the response is minified before it is compressed. For example:
Minimum configuration (all types enabled with default settings):
http://localhost:9200 {
route {
encode zstd gzip
minifier
reverse_proxy localhost:8097
}
}Disable some content types:
http://localhost:9200 {
route {
minifier {
disable js svg
}
file_server
}
}Full configuration:
http://localhost:9200 {
route {
minifier {
html {
KeepSpecialComments true
KeepConditionalComments true
KeepComments true
KeepWhitespace true
KeepDefaultAttrVals true
KeepDocumentTags true
KeepEndTags true
KeepQuotes true
TemplateDelims {{ }}
}
css {
Precision 0
Inline false
Version 0
}
js {
Precision 0
KeepVarNames true
Version 0
}
svg {
KeepComments true
Precision 0
KeepNamespaces inkscape sodipodi
}
}
reverse_proxy localhost:8097
}
}Options map 1:1 to the exported fields of the tdewolff/minify Minifier structs, see html, css, js and svg. Unknown options or invalid values are rejected when the config is loaded.
JSON configuration uses the same names:
{
"handler": "minifier",
"disable": ["svg"],
"Html": { "KeepComments": true },
"Css": { "Precision": 3 },
"Js": { "KeepVarNames": true },
"Svg": { "KeepComments": true }
}Special thanks to @mholt for making a good documentation on how to use unstandard Caddy plugins, mholt/caddy-webdav