Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves static web assets from a directory. It supports in-memory caching, on-the-fly JS/CSS minification, and virtual bundles that concatenate multiple source files.
Usage:
h := webassets.New("./web/static",
webassets.WithCache(),
webassets.WithMinification(),
webassets.WithBundle("app.bundle.js", "js/vendor.js", "js/app.js"),
)
mux.Handle("/assets/", http.StripPrefix("/assets", h))
type Option ¶
type Option func(*Handler)
Option configures a Handler.
func WithBundle ¶
WithBundle registers a virtual bundle file accessible at /<name>. When requested, the source files are read from the handler root directory, concatenated in order, and optionally minified.
func WithCache ¶
WithCache enables in-memory caching. Each asset is read from disk on the first request and served from memory on all subsequent requests.
func WithCacheExclude ¶
WithCacheExclude prevents paths with the given URL prefixes from being cached. Only applies when WithCache is also used.
func WithErrorLogger ¶
WithErrorLogger sets a function that is called whenever an error is handled gracefully (e.g. a missing file, a failed minification). The handler always recovers and returns a safe HTTP response; this just gives you visibility. Example: webassets.WithErrorLogger(func(err error) { slog.Error(err.Error()) })
func WithMinification ¶
WithMinification enables on-the-fly minification of JavaScript and CSS. Minification errors are silently ignored and the original content is served.