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

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