Static Files
Serve a directory of static assets from a URL prefix.
Static Files
app.Static serves files from a directory under a URL prefix.
app := ply.New()
app.Static("/assets", "./public")
app.RunServer(":8080")A request for /assets/app.css reads ./public/app.css. The prefix is
normalized to start with / and trailing slashes are removed.
Behavior
Static registers GET routes for:
- the prefix itself,
- the prefix with a trailing slash,
- the prefix followed by any path.
It uses fasthttp's file server with:
| Setting | Value |
|---|---|
| Index file | index.html |
| Directory listing | disabled |
| Compression | gzip and Brotli enabled |
| Cache duration | 10 seconds |
| Methods | GET |
/assets, /assets/, and /assets/logo.svg are all valid.
Middleware
Static routes are registered as fast routes. They do not run middleware added
with app.Use, including authentication and request logging. Do not expose
private files through Static. Put protected downloads behind a normal route
that checks the user, or protect the prefix at the reverse proxy.
Directory scope
Point Static at a dedicated asset directory, never at the repository root:
app.Static("/assets", "./public")The file server exposes every file under the root, including dotfiles if they exist. Keep secrets, source files, and configuration outside that directory.
Cache behavior
The built-in cache duration is short. For fingerprinted assets, set a longer cache at the reverse proxy or a CDN:
location /assets/ {
proxy_pass http://127.0.0.1:8080;
add_header Cache-Control "public, max-age=31536000, immutable";
}Use immutable caching only for names that change when the content changes.
Custom routes
For a single file or a route that needs middleware, use a normal handler:
app.Get("/robots.txt", func(c *core.Context) {
c.Ctx.SendFile("public/robots.txt")
})
Ply