static-assets v0.1.1

Production static asset HTTP::Handler for Crystal with immutable caching, ETag/304, Brotli/Gzip precompression, byte ranges, and MemoryStore

static-assets

CI Docs License

Production static asset HTTP::Handler for Crystal (>= 1.16.0) with zero runtime dependencies.

Built to complement vite-crystal and vite-plugin-crystal, static-assets adds what Crystal's stdlib HTTP::StaticFileHandler leaves out: immutable long-lived caching for fingerprinted bundles, precompressed .br/.gz serving, ETag / Last-Modified 304 validation, HTTP byte ranges (206 / 416), and an optional in-memory single-binary asset store.

Features

  • Vite-ready cache headers: Serves /build/assets/* (configurable via immutable_paths and immutable_pattern) with Cache-Control: public, max-age=31536000, immutable, and non-fingerprinted files with public, max-age=0, must-revalidate.
  • Precompressed .br & .gz negotiation: RFC 9110 Accept-Encoding (br, gzip, x-gzip, q-values) automatically serves sidecar .br/.gz files when present, sets Content-Encoding + Vary: Accept-Encoding, and scopes ETag per encoding (-br, -gzip) to prevent cache collisions.
  • Conditional 304 Not Modified: Evaluates If-None-Match (strong/weak/comma lists/*) and If-Modified-Since per RFC 9110.
  • Byte Range support (206 / 416): Handles Range (bytes=start-end, bytes=start-, bytes=-suffix), Accept-Ranges: bytes, and If-Range validators.
  • Filesystem security: Normalizes URL paths, rejects path-traversal escapes (400 Bad Request), and verifies File.realpath stays strictly inside public_dir for both assets and .br/.gz sidecars.
  • Single-binary ready (MemoryStore): Load an entire asset directory (MemoryStore.from_dir) or embed compile-time files (StaticAssets.embed_file) with SHA-256 strong ETags and paired .br/.gz bytes.

Installation

Add static_assets to your shard.yml:

dependencies:
  static_assets:
    github: spcbeck/static-assets
    version: ~> 0.1.0

Run shards install, then require "static_assets".

Usage

Disk Serving with HTTP::Server & vite-crystal

require "http/server"
require "static_assets"
require "vite"

assets = StaticAssets.handler("public") do |config|
  # Defaults already cover Vite's `/build/assets/`:
  # config.immutable_paths = ["/build/assets/"]
  # config.immutable_cache_control = "public, max-age=31536000, immutable"
  # config.default_cache_control = "public, max-age=0, must-revalidate"
  # config.precompressed = true
end

server = HTTP::Server.new([assets]) do |context|
  context.response.content_type = "text/html; charset=utf-8"
  context.response.print(<<-HTML)
    <!doctype html>
    <html>
      <head>#{Vite.tags("src/main.ts")}</head>
      <body><h1>Hello</h1></body>
    </html>
    HTML
end

server.bind_tcp("127.0.0.1", 3000)
server.listen

Single-Binary In-Memory Serving (MemoryStore)

require "static_assets"

store = StaticAssets::MemoryStore.new
  .add("build/assets/app.js", StaticAssets.embed_file("public/build/assets/app.js"))

handler = StaticAssets.handler(store: store)

Or load a directory at boot (including .br / .gz sidecars):

store = StaticAssets::MemoryStore.from_dir("public")
handler = StaticAssets.handler(store: store)

Subpath Mounting & Custom Headers

handler = StaticAssets.handler("public", url_prefix: "/assets") do |config|
  config.immutable_pattern = /\.[a-f0-9]{8,}\./i
  config.index_file = "index.html"
  config.headers = {"Cross-Origin-Resource-Policy" => "cross-origin"}
end

Configuration Options (StaticAssets::Config)

Property Default Description
public_dir "public" Root directory for DiskStore when no custom store is supplied.
url_prefix "/" Request path prefix handled by StaticAssets::Handler.
immutable_paths ["/build/assets/"] Request path prefixes receiving immutable_cache_control.
immutable_pattern nil Optional Regex for matching fingerprinted paths.
immutable_cache_control "public, max-age=31536000, immutable" Cache-Control header for immutable paths.
default_cache_control "public, max-age=0, must-revalidate" Cache-Control header for non-immutable paths.
precompressed true Serve .br / .gz sidecars based on Accept-Encoding.
etag true Emit ETag headers and honor If-None-Match.
last_modified true Emit Last-Modified headers and honor If-Modified-Since.
fallthrough true Pass misses to call_next; when false, responds 404 Not Found.
index_file nil Optional directory index filename (e.g. "index.html").
headers nil Optional extra response headers (Hash(String, String)).

License

MIT

Repository

static-assets

Owner
Statistic
  • 0
  • 0
  • 0
  • 0
  • 1
  • 11 minutes ago
  • December 6, 2026
License

MIT License

Links
Synced at

Sun, 06 Dec 2026 08:48:15 GMT

Languages