Static Files¶
Content-hashed static file serving with cache-busting URLs, similar to Django's ManifestStaticFilesStorage.
Package: github.com/oliverandrich/burrow/contrib/staticfiles
Depends on: none
Setup¶
import "embed"
//go:embed static
var staticFS embed.FS
sfApp, err := staticfiles.New(staticFS)
if err != nil {
log.Fatal(err)
}
srv := burrow.NewServer(
sfApp,
// ... other apps
)
How It Works¶
- At startup, the app walks the embedded filesystem and computes a SHA-256 hash for each file
- Files are served under hashed URLs:
styles.cssbecomesstyles.a1b2c3d4.css - Hashed URLs get
Cache-Control: public, max-age=31536000, immutable(1 year) - Non-hashed URLs get
Cache-Control: no-cache, no-store, must-revalidate
Generating URLs¶
Use staticfiles.URL() in templates to resolve hashed paths:
In HTML templates (via HasFuncMap):
{{ define "app/layout" -}}
<link rel="stylesheet" href="{{ staticURL "styles.css" }}">
<script src="{{ staticURL "app.js" }}" defer></script>
{{- end }}
If the file is not found in the manifest, the original name is returned as-is (safe fallback).
Custom Prefix¶
By default, files are served at /static/. Change it with WithPrefix:
File Organization¶
All files are walked recursively. The manifest maps original paths to hashed paths:
| Original | Hashed |
|---|---|
styles.css |
styles.a1b2c3d4.css |
images/logo.png |
images/logo.e5f6a7b8.png |
App-Contributed Static Files¶
Contrib apps can contribute their own CSS/JS by implementing HasStaticFiles:
//go:embed static
var adminStaticFS embed.FS
func (a *App) StaticFS() (string, fs.FS) {
sub, _ := fs.Sub(adminStaticFS, "static")
return "admin", sub
}
The staticfiles app automatically discovers all HasStaticFiles implementations during Register() and serves their files under the declared prefix:
Generate URLs with the prefix included:
Files from all sources get the same content-hashing and cache headers.
Interfaces Implemented¶
| Interface | Description |
|---|---|
burrow.App |
Required: Name(), Register() |
HasRoutes |
Static file serving route |
HasMiddleware |
Context injection and cache header middleware |
HasFuncMap |
Provides staticURL template function |