Documentation
¶
Overview ¶
Package spa provides an http.Handler that serves single-page applications.
Static assets are served directly from an io/fs.FS; all other request paths fall back to the index file so that client-side routing works without server-side route configuration.
Basic usage ¶
//go:embed dist var distFS embed.FS sub, _ := fs.Sub(distFS, "dist") handler := spa.Handler(sub)
Runtime environment injection ¶
If your index file contains a placeholder string, WithEnvVars replaces it with a JSON object at request time:
handler := spa.Handler(sub,
spa.WithEnvVars("__FRONTEND_ENV__", map[string]any{
"API_URL": os.Getenv("API_URL"),
}),
)
Custom options ¶
handler := spa.Handler(sub,
spa.WithIndexFile("shell.html"),
spa.WithIndexCacheControl("no-cache"),
spa.WithNotFoundHandler(http.NotFoundHandler()),
)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Option ¶
type Option func(*options)
Option configures a SPA handler.
func WithEnvVars ¶
WithEnvVars injects runtime environment variables into the index file. Every occurrence of placeholder in the index file is replaced with a JSON-encoded representation of vars before the response is written.
func WithIndexCacheControl ¶
WithIndexCacheControl sets the Cache-Control header on index file responses. Defaults to "no-cache, no-store, must-revalidate".
func WithIndexFile ¶
WithIndexFile sets the path within the FS used as the SPA entry point. Defaults to "index.html".
func WithNotFoundHandler ¶
WithNotFoundHandler sets the handler called when a requested path does not exist as a static asset. Defaults to serving the index file (SPA fallback).
func WithStaticCacheControl ¶
WithStaticCacheControl sets the Cache-Control header on static asset responses. For SPAs with content-hashed filenames, "public, max-age=31536000, immutable" is typical. If unset, no Cache-Control header is added (http.FileServerFS handles ETags/Last-Modified).