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.

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
  protected def cache_control(request_path : String) : String
    return IMMUTABLE_CACHE_CONTROL if Static.fingerprinted?(request_path, hex_lengths: 8..8)
    super
  end
end

Defined in:

amber/pipes/static.cr

Constant Summary

FINGERPRINT_HEX_LENGTHS = 16..64

How many hex digits a content-addressed filename carries after its dash.

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

Class Method Summary

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]

Class Method Detail

def self.fingerprinted?(path : String, hex_lengths : Range(Int32, Int32) = FINGERPRINT_HEX_LENGTHS) : Bool #

Returns true when the last segment of path holds a dash, then a run of hex digits (either case) whose length is in hex_lengths, then a dot with at least one character after it, as in app-0123456789abcdef.css or app-0123456789abcdef.min.css.

Amber::Pipe::Static.fingerprinted?("/assets/app-0123456789abcdef.css") # => true
Amber::Pipe::Static.fingerprinted?("/assets/app.css")                  # => false
Amber::Pipe::Static.fingerprinted?("/a/app-0123abcd.js", 8..8)         # => true

[View source]

Instance Method Detail

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

[View source]