static-assets v0.1.1
static-assets
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 viaimmutable_pathsandimmutable_pattern) withCache-Control: public, max-age=31536000, immutable, and non-fingerprinted files withpublic, max-age=0, must-revalidate. - Precompressed
.br&.gznegotiation: RFC 9110Accept-Encoding(br,gzip,x-gzip,q-values) automatically serves sidecar.br/.gzfiles when present, setsContent-Encoding+Vary: Accept-Encoding, and scopesETagper encoding (-br,-gzip) to prevent cache collisions. - Conditional
304 Not Modified: EvaluatesIf-None-Match(strong/weak/comma lists/*) andIf-Modified-Sinceper RFC 9110. - Byte Range support (
206/416): HandlesRange(bytes=start-end,bytes=start-,bytes=-suffix),Accept-Ranges: bytes, andIf-Rangevalidators. - Filesystem security: Normalizes URL paths, rejects path-traversal escapes (
400 Bad Request), and verifiesFile.realpathstays strictly insidepublic_dirfor both assets and.br/.gzsidecars. - Single-binary ready (
MemoryStore): Load an entire asset directory (MemoryStore.from_dir) or embed compile-time files (StaticAssets.embed_file) with SHA-256 strongETags and paired.br/.gzbytes.
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
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