Documentation
¶
Overview ¶
Package vite integrates a Vite-built frontend into a Go server: it resolves a Vite build manifest to hashed asset URLs in production, points at the Vite dev server in development, serves the embedded production assets, and notifies the Vite dev server to reload — all behind one dev boolean.
It depends only on the standard library and on Vite's manifest format; it has no knowledge of any Go HTML templating library.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewContext ¶ added in v0.2.0
NewContext returns a copy of ctx carrying v, retrievable with FromContext.
func NotifyReload ¶
func NotifyReload(devURL string)
NotifyReload POSTs to <devURL>/__reload so a Vite plugin exposing that endpoint broadcasts a browser full-reload. Call it once after the HTTP server's listeners are up. Dev-only: a "" devURL is a no-op. Runs in a goroutine with a brief retry loop (covers the cold-start race where the Go server beats Vite to the port).
Types ¶
type Bundle ¶
Bundle is the resolved asset URL list for one entry. The caller renders these into <script>/<link>/<link rel=modulepreload> tags however it likes.
type Config ¶
type Config struct {
DevURL string // running Vite dev server origin; "" → prod
DevBase string // dev base path (vite.config base); default "/"
Dist fs.FS // embedded prod build output (holds .vite/manifest.json); required in prod
DistDir string // subpath within Dist for manifest+assets; default "."
StaticURL string // URL prefix prod assets serve under; default "/static/"
// Entries optionally maps a friendly entry name to its source path (the
// manifest key, e.g. package.json entryPoints). When set, Entry(name)
// resolves name → source before dev/prod lookup. nil → name is used as-is.
Entries map[string]string
}
Config configures a Vite integration. The zero DevURL selects prod mode.
type Vite ¶
type Vite struct {
// contains filtered or unexported fields
}
Vite resolves Vite entries to asset URLs. Safe for concurrent use; build once at startup and share across requests.
func FromContext ¶ added in v0.2.0
FromContext returns the *Vite stored by NewContext, or nil if none is present. Templates and handlers use it to read the asset bundle from the request context instead of receiving it as a parameter.
func New ¶
New builds a *Vite. In prod (DevURL == "") it reads and parses the manifest from Dist and returns an error if Dist is nil or the manifest is missing or invalid. In dev it performs no I/O.
func (*Vite) Entry ¶
Entry resolves one Vite entry to its asset URLs. name may be a friendly name registered in Config.Entries (resolved to its source path) or, when absent from Entries (or Entries is nil), the manifest key / source path directly. Never panics; an unknown prod entry yields an empty Bundle.
func (*Vite) Middleware ¶ added in v0.2.0
Middleware injects v into each request's context (via NewContext) before calling next, so downstream handlers and rendered templates can retrieve it with FromContext.
func (*Vite) StaticHandler ¶
StaticHandler serves the embedded prod assets (Config.Dist, rooted at DistDir) under Config.StaticURL. In dev it has no assets to serve and returns a not-found handler; mount it only in prod (or always — /static/ is never hit in dev). The StaticURL prefix is stripped without its trailing slash so the remaining request path keeps its leading slash for the file server.