Skip to content

Instantly share code, notes, and snippets.

@monochromer
Created December 14, 2024 16:53
Show Gist options
  • Save monochromer/135afb8bc7289ffecba50f457258a519 to your computer and use it in GitHub Desktop.
Save monochromer/135afb8bc7289ffecba50f457258a519 to your computer and use it in GitHub Desktop.
Caddy server autoserving `avif` and `webp` images. For example, instead of `image.png` serve `image.png.avif` if browser support it.
https://localhost:8443 {
tls internal
vars {
staticFolder "./public"
}
map {header.Accept} {avifSuffix} {
~image/avif avif
default ''
}
map {header.Accept} {webpSuffix} {
~image/webp webp
default ''
}
@static {
path *.css *.js *.gif *.jpg *.jpeg *.png *.avif *.webp *.svg *.woff2 *.woff
}
header @static Cache-Control "public, immutable, max-age=5184000"
@images-to-rewrite {
path_regexp \.(jpg|jpeg|png|gif)$
}
handle @images-to-rewrite {
header {
Vary Accept
}
root {vars.staticFolder}
try_files {path}.{avifSuffix} {path}.{webpSuffix} {path} =404
}
file_server {
root {vars.staticFolder}
precompressed br gzip zstd
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment