StaticFilesServer class
Serves static files from a directory with security, caching, and SPA support.
Features:
- Secure path traversal protection
- MIME type detection (50+ types)
- Cache headers (
ETag,Last-Modified,Cache-Control) - 304 Not Modified support
HEADrequest optimization- SPA fallback to
index.html - Directory
index.htmlauto-serve
Usage:
final staticServer = StaticFilesServer(
root: Directory('public'),
maxAgeSeconds: 86400,
spaFallback: true,
);
// In middleware:
if (await staticServer.tryServe(request)) return;
Constructors
- StaticFilesServer({Directory? root, int maxAgeSeconds = 3600, bool spaFallback = false})
- Creates a static file server.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- maxAgeSeconds → int
-
Cache duration in seconds for
Cache-Control: public, max-age=....final - root → Directory
-
Root directory to serve files from.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- spaFallback → bool
-
Enables SPA mode: non-file routes return
index.html.final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
tryServe(
HttpRequest req) → Future< bool> - Attempts to serve a static file for the given HttpRequest.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited