class
Amber::Pipe::Static
- Amber::Pipe::Static
- HTTP::StaticFileHandler
- Reference
- Object
Overview
Serves static files from the given public directory.
Amber keeps directory listings disabled, applies the configured static
response headers, and gives content-addressed files an immutable cache
policy. A content-addressed filename ends in a SHA-256 fragment, for
example app-0123456789abcdef.css.
An application with its own cache policy, for example a fingerprint
scheme other than Amber's, overrides #cache_control in a subclass. The
hook runs only for a file this pipe serves. A Cache-Control that an
earlier handler already set on the response is kept as it is.
class AppStatic < Amber::Pipe::Static
SHORT_FINGERPRINT = /-[0-9a-f]{8}\.[^\/]+$/i
protected def cache_control(request_path : String) : String
return IMMUTABLE_CACHE_CONTROL if request_path.matches?(SHORT_FINGERPRINT)
super
end
end
Defined in:
amber/pipes/static.crConstant Summary
-
FINGERPRINT_PATTERN =
/-[0-9a-f]{16,64}\.[^\/]+$/i -
IMMUTABLE_CACHE_CONTROL =
"public, max-age=31536000, immutable" -
MIME_TYPES =
{".avif" => "image/avif", ".cjs" => "text/javascript; charset=utf-8", ".csv" => "text/csv; charset=utf-8", ".css" => "text/css; charset=utf-8", ".eot" => "application/vnd.ms-fontobject", ".gif" => "image/gif", ".htm" => "text/html; charset=utf-8", ".html" => "text/html; charset=utf-8", ".ico" => "image/x-icon", ".jpeg" => "image/jpeg", ".jpg" => "image/jpeg", ".js" => "text/javascript; charset=utf-8", ".json" => "application/json; charset=utf-8", ".map" => "application/json; charset=utf-8", ".mjs" => "text/javascript; charset=utf-8", ".mp3" => "audio/mpeg", ".mp4" => "video/mp4", ".ogg" => "audio/ogg", ".otf" => "font/otf", ".pdf" => "application/pdf", ".png" => "image/png", ".svg" => "image/svg+xml", ".ttf" => "font/ttf", ".txt" => "text/plain; charset=utf-8", ".wasm" => "application/wasm", ".wav" => "audio/wav", ".webm" => "video/webm", ".webmanifest" => "application/manifest+json; charset=utf-8", ".webp" => "image/webp", ".woff" => "font/woff", ".woff2" => "font/woff2", ".xml" => "application/xml; charset=utf-8", ".zip" => "application/zip"} -
MIME registrations Crystal cannot consistently discover from the host operating system. Keeping them here makes static responses portable across macOS, Linux, and Windows.
-
REVALIDATE_CACHE_CONTROL =
"no-cache"
Constructors
-
.new(public_dir : String, fallthrough = false, directory_listing = false, headers : Hash(String, String) | Nil = nil)
Sets the default behavior for static file serving to NOT list the directory contents.
Instance Method Summary
Constructor Detail
Sets the default behavior for static file serving to NOT list the
directory contents. Headers from Amber.settings.static.headers are
used unless an explicit hash is supplied.