Documentation
¶
Overview ¶
Package ssg prerenders an http.Handler to static files.
Generate performs one in-process GET per page and writes only a 200 response. It does not open a socket, follow redirects, crawl registered routes, or run the Inertia Node SSR server (that stays in inertia/ssr.go). The request carries no cookies. Its host defaults to DefaultHost. A handler can tell the call apart with FromContext and should not render per-user data: every visitor of the static file would see it.
report, err := ssg.Generate(ctx, handler, []ssg.Page{
{Path: "/"},
{Path: "/about"},
}, ssg.Options{OutDir: "dist"})
Index ¶
Constants ¶
const ( // DefaultHost is the request host when [Options.Host] is empty. // .invalid is a reserved TLD, so the value is never a real origin. DefaultHost = "ssg.invalid" // MaxBody is the largest response body Generate will store. MaxBody = 8 << 20 )
Variables ¶
var ( // ErrInvalidPath reports a page path that is not a safe absolute URL path. ErrInvalidPath = errors.New("ssg: invalid path") // ErrStatus reports a response that is not 200. Nothing was written for that page. ErrStatus = errors.New("ssg: response status is not 200") // ErrTooLarge reports a body larger than [MaxBody]. Nothing was written for that page. ErrTooLarge = errors.New("ssg: body exceeds 8 MiB") // ErrEscape reports an output path that would leave [Options.OutDir]. ErrEscape = errors.New("ssg: path escapes the output directory") )
Functions ¶
func FromContext ¶
FromContext reports whether ctx was created by NewContext.
func NewContext ¶
NewContext marks ctx as a prerender. A nil parent uses context.Background.
Types ¶
type File ¶
type File struct {
// Path is relative to OutDir and uses forward slashes.
Path string
// Bytes is the size of the file.
Bytes int64
}
File is one written page.
type Options ¶
type Options struct {
// OutDir is the directory to create and fill. Required.
OutDir string
// Host is the request host. Empty means [DefaultHost].
Host string
}
Options selects where files are written and which host the request uses.
type Page ¶
type Page struct {
// Path is an absolute URL path, such as "/" or "/about".
// A scheme, host, query, fragment, NUL, backslash or ".." segment is rejected.
Path string
}
Page is one URL path to prerender.
type Report ¶
type Report struct {
Files []File
}
Report lists the pages Generate wrote, in request order. On error it still lists pages written before the failure. Those files are left in place. The failing page is not written.
func Generate ¶
Generate writes one index.html per page. "/" becomes index.html, "/about" becomes about/index.html and "/docs/cli" becomes docs/cli/index.html. OutDir is created when every path is valid. A non-200 status, including a redirect, or a body over MaxBody, writes nothing for that page and stops the run.