class Amber::Pipe::Static

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.

Defined in:

amber/pipes/static.cr

Constant 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

Instance Method Summary

Constructor Detail

def self.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. Headers from Amber.settings.static.headers are used unless an explicit hash is supplied.


[View source]

Instance Method Detail

def call(context : HTTP::Server::Context) : Nil #

[View source]