inertia package (from gocopper/pkg) provides the server-side adapter, wired in automatically by copper create.
Rendering a Page
Inject*inertia.Renderer into your router and call Render with a component name and props:
pkg/rockets/router.go
web/src/pages/ and receive props directly:
web/src/pages/rockets.tsx
Forms
You may read and validate a form submission usingReadForm, which decodes the JSON body and validates its valid: tags (govalidator). On validation failure, the error is flashed automatically and ReadForm returns false, so you simply redirect back:
Redirect303, and the next render picks up any flashed props. Validation errors appear on the client under flash.validationError.
Flash Props
FlashProps stores props for a single upcoming render, which is perfect for success and error messages across a redirect:
flash prop, after which they are gone:
Shared Props
ShareProps adds props to the current request’s render from anywhere. It is typically called from a middleware so that every page in a section receives common data:
Middlewares, or globally in pkg/app/handler.go.
ShareProps identifies the request via its request ID, so chttp.SetRequestIDInCtxMiddleware() must be in your global middlewares. Scaffolded projects include it by default. Successive calls for the same request replace previously shared props rather than merging them, so you should share everything in a single call.Partial Reloads
When the client reloads only specific props (router.reload({ only: ["rockets"] })), you may skip computing everything else with ShouldLoadProp:
ShouldLoadProp returns true on full renders and only for the requested props on partial reloads.
Deferred Props
Deferred props let a page render immediately while slow data loads in a follow-up request. Declare them inDeferredProps, and guard the expensive work with ShouldLoadDeferredProp:
ShouldLoadProp, ShouldLoadDeferredProp returns false on the initial render: the prop is stripped from the first response and fetched immediately afterwards. On the client, wrap the slow section in Inertia’s <Deferred>:
Customizing the Renderer
The renderer is immutable; eachWith* method returns a copy, so you may configure it once in your constructor:
WithBasePath(path)mounts a section under a URL prefix. Redirects and page URLs have the prefix stripped so that client-side paths stay clean; it pairs withchttp.base_path.WithLayoutTemplate(name)renders pages inside a different layout thanmain.html.WithComponent(name)sets a default component for all renders.
RenderParams accepts per-render LayoutTemplate and BasePath overrides too.