bond

package
v0.9.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotInitialized   = errors.New("bond: not initialized")
	ErrInvalidTemplate  = errors.New("bond: invalid template - must contain {{ .inertia }}")
	ErrTemplateRequired = errors.New("bond: template is required")
)

Errors

Functions

This section is empty.

Types

type AlwaysProp

type AlwaysProp struct {
	// contains filtered or unexported fields
}

AlwaysProp is always included in responses, even during partial reloads

func Always

func Always(value any) AlwaysProp

Always creates a prop that is always included, even in partial reloads

func (AlwaysProp) Value

func (a AlwaysProp) Value() any

Value returns the always prop's value

type Bond

type Bond struct {
	// contains filtered or unexported fields
}

Bond is the main Inertia handler

func New

func New(config Config) (*Bond, error)

New creates a new Bond instance

func (*Bond) Back

func (b *Bond) Back(w http.ResponseWriter, r *http.Request)

Back redirects to the previous page using the Referer header. Only allows relative URLs or URLs matching the request host. Falls back to "/" if no Referer is present or if it points to an external domain.

func (*Bond) ClearShared

func (b *Bond) ClearShared()

ClearShared removes all shared props (useful for testing)

func (*Bond) ContainerID

func (b *Bond) ContainerID() string

ContainerID returns the configured container ID

func (*Bond) DecryptHistoryState

func (b *Bond) DecryptHistoryState(encrypted string) (*Page, error)

DecryptHistoryState decrypts page data from encrypted browser history

func (*Bond) EncryptHistoryState

func (b *Bond) EncryptHistoryState(page Page) (string, error)

EncryptHistoryState encrypts page data for secure browser history storage Returns encrypted string or empty string if encryption is disabled

func (*Bond) Location

func (b *Bond) Location(w http.ResponseWriter, r *http.Request, url string)

Location forces a full page reload (external redirect) This breaks out of the SPA and performs a full navigation Use for external URLs or when you need to break out of Inertia

func (*Bond) Middleware

func (b *Bond) Middleware(next http.Handler) http.Handler

Middleware returns HTTP middleware for Inertia protocol handling It performs version checking and sets appropriate headers

func (*Bond) MiddlewareFunc

func (b *Bond) MiddlewareFunc() router.MiddlewareFunc

MiddlewareFunc returns Velocity router middleware

func (*Bond) Redirect

func (b *Bond) Redirect(w http.ResponseWriter, r *http.Request, url string)

Redirect performs an SPA-compatible redirect Uses 303 See Other for POST-Redirect-GET pattern

func (*Bond) RedirectWithStatus

func (b *Bond) RedirectWithStatus(w http.ResponseWriter, r *http.Request, rawURL string, status int)

RedirectWithStatus performs a redirect with a custom status code. The URL is validated to prevent open redirects: only relative paths and same-host URLs are allowed. Use Location() for external redirects.

func (*Bond) Render

func (b *Bond) Render(w http.ResponseWriter, r *http.Request, component string, props Props) error

Render renders a component with props

func (*Bond) SetEncryptor added in v0.9.11

func (b *Bond) SetEncryptor(enc interface {
	Encrypt(string) (string, error)
	Decrypt(string) (string, error)
})

SetEncryptor sets the encryptor used for history state encryption.

func (*Bond) SetSharePropsFunc added in v0.9.5

func (b *Bond) SetSharePropsFunc(fn func(r *http.Request) (Props, error))

SetSharePropsFunc sets a function that returns props to be shared per request.

func (*Bond) Share

func (b *Bond) Share(key string, value any)

Share adds a static prop to all responses

func (*Bond) ShareFunc

func (b *Bond) ShareFunc(key string, fn SharedPropFunc)

ShareFunc adds a dynamic prop evaluated per-request

func (*Bond) ShareMultiple

func (b *Bond) ShareMultiple(props Props)

ShareMultiple adds multiple static props at once

func (*Bond) Version

func (b *Bond) Version() string

Version returns the configured asset version

type Config

type Config struct {
	RootTemplate   string // Full HTML template with {{ .inertia }} placeholder
	Version        string // Asset version for cache busting
	ContainerID    string // Default: "app"
	EncryptHistory bool   // Use pkg/crypto for history state encryption
}

Config holds bond configuration

type DeferredProp

type DeferredProp struct {
	// contains filtered or unexported fields
}

DeferredProp is loaded after the initial page render by the client

func Defer

func Defer(fn func() (any, error), group ...string) DeferredProp

Defer creates a deferred prop with an optional group name Deferred props are not included in the initial response but are fetched by the client after the page renders

func (DeferredProp) Evaluate

func (d DeferredProp) Evaluate() (any, error)

Evaluate resolves the deferred prop value

func (DeferredProp) Group

func (d DeferredProp) Group() string

Group returns the deferred prop's group name

type LazyProp

type LazyProp struct {
	// contains filtered or unexported fields
}

LazyProp is evaluated only when explicitly requested via partial reload

func Lazy

func Lazy(fn func() (any, error)) LazyProp

Lazy creates a lazy prop that is only evaluated when explicitly requested

func (LazyProp) Evaluate

func (l LazyProp) Evaluate() (any, error)

Evaluate resolves the lazy prop value

type OptionalProp

type OptionalProp struct {
	// contains filtered or unexported fields
}

OptionalProp is excluded from the first visit unless explicitly requested Similar to LazyProp but semantically different for API clarity

func Optional

func Optional(fn func() (any, error)) OptionalProp

Optional creates an optional prop (same behavior as Lazy)

func (OptionalProp) Evaluate

func (o OptionalProp) Evaluate() (any, error)

Evaluate resolves the optional prop value

type Page

type Page struct {
	Component      string              `json:"component"`
	Props          Props               `json:"props"`
	URL            string              `json:"url"`
	Version        string              `json:"version"`
	EncryptHistory bool                `json:"encryptHistory,omitempty"`
	ClearHistory   bool                `json:"clearHistory,omitempty"`
	DeferredProps  map[string][]string `json:"deferredProps,omitempty"`
}

Page represents an Inertia page response

func (Page) ToHTMLAttr

func (p Page) ToHTMLAttr() (string, error)

ToHTMLAttr returns JSON escaped for use in HTML data attributes Single quotes in the JSON are escaped since we use data-page='...'

func (Page) ToJSON

func (p Page) ToJSON() (string, error)

ToJSON returns the JSON representation of the page

type Props

type Props map[string]any

Props holds component properties

type SharedPropFunc

type SharedPropFunc func(r *http.Request) (any, error)

SharedPropFunc is evaluated per-request for dynamic shared props

type TemplateData

type TemplateData struct {
	Inertia     template.HTML // The Inertia div with data-page attribute
	InertiaHead template.HTML // Head content for SSR (empty for CSR)
}

TemplateData holds data passed to the root HTML template

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL