unpoly

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 5 Imported by: 0

README

Unpoly Plugin for Nimbus

Unpoly integrates server-driven partial page updates with Nimbus. The plugin adds middleware helpers for Unpoly request/response headers.

Install

import "github.com/CodeSyncr/nimbus/plugins/unpoly"

app.Use(unpoly.New())

What it does

  • Adds protocol headers for Unpoly requests
  • Exposes helpers to inspect request context and set response behaviors
  • Keeps server-rendered templates as the primary rendering model

Default config

From DefaultConfig():

  • enabled: true
  • cdn: https://unpoly.com
  • version: 3.12.0

Typical usage

app.Router.Get("/posts/:id", func(c *http.Context) error {
    if unpoly.IsFragmentRequest(c) {
        return c.View("posts/_show", data)
    }
    return c.View("posts/show", data)
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcceptLayer

func AcceptLayer(c *http.Context, value any)

AcceptLayer closes the targeted overlay and accepts it with the given value (X-Up-Accept-Layer, serialized as JSON). The value becomes the overlay's acceptance value.

func Context

func Context(c *http.Context) map[string]any

Context returns the targeted layer's context object from X-Up-Context (parsed from JSON), or nil.

func DismissLayer

func DismissLayer(c *http.Context, value any)

DismissLayer closes the targeted overlay and dismisses it with the given value (X-Up-Dismiss-Layer, serialized as JSON).

func EmitEvent

func EmitEvent(c *http.Context, eventType string, props map[string]any)

EmitEvent sends a client-side event via X-Up-Events. The event is serialized as JSON. Call multiple times to emit multiple events.

func EvictCache

func EvictCache(c *http.Context, pattern string)

EvictCache tells Unpoly to evict (delete) cached responses matching the given URL pattern (X-Up-Evict-Cache). Use "*" to evict all.

func ExpireCache

func ExpireCache(c *http.Context, pattern string)

ExpireCache tells Unpoly to expire cached responses matching the given URL pattern (X-Up-Expire-Cache). Use "*" to expire all. Expired entries are re-validated on next use.

func FailContext

func FailContext(c *http.Context) map[string]any

FailContext returns the layer context for a failed update (X-Up-Fail-Context).

func FailMode

func FailMode(c *http.Context) string

FailMode returns the mode for a failed fragment update (X-Up-Fail-Mode).

func FailTarget

func FailTarget(c *http.Context) string

FailTarget returns the CSS selector for a failed update (X-Up-Fail-Target).

func IsUnpoly

func IsUnpoly(c *http.Context) bool

IsUnpoly returns true if the request was made by Unpoly (has X-Up-Version).

func IsValidating

func IsValidating(c *http.Context) bool

IsValidating returns true if Unpoly is validating form fields.

func Mode

func Mode(c *http.Context) string

Mode returns the targeted layer's mode (X-Up-Mode): "root", "modal", "popup", "drawer", or "cover".

func RenderNothing

func RenderNothing(c *http.Context)

RenderNothing tells Unpoly to skip rendering by setting X-Up-Target: :none. The response body can be empty.

func ServerProtocol

func ServerProtocol() router.Middleware

ServerProtocol returns middleware that implements the Unpoly server protocol. For every request that carries an X-Up-Version header it:

  • Echoes X-Up-Location with the current request URL
  • Echoes X-Up-Method with the current HTTP method
  • Adds Vary: X-Up-Target, X-Up-Version for cache partitioning

Non-Unpoly requests pass through untouched.

func SetLocation

func SetLocation(c *http.Context, url string)

SetLocation overrides the browser location shown after a fragment update (X-Up-Location). Useful after redirects or rewrites.

func SetMethod

func SetMethod(c *http.Context, method string)

SetMethod overrides the HTTP method echoed back to Unpoly (X-Up-Method). Useful after method-changing redirects.

func SetTarget

func SetTarget(c *http.Context, selector string)

SetTarget overrides the CSS selector that Unpoly will update (X-Up-Target response header). Useful when the server wants to render a different fragment than what was requested.

func SetTitle

func SetTitle(c *http.Context, title string)

SetTitle changes the document title after a fragment update (X-Up-Title). The value is JSON-encoded as Unpoly expects. Set to "false" to prevent Unpoly from changing the title.

func Target

func Target(c *http.Context) string

Target returns the CSS selector Unpoly is targeting (X-Up-Target), or an empty string for non-Unpoly / full-page requests.

func Validate

func Validate(c *http.Context) string

Validate returns the names of form fields being validated (X-Up-Validate), or an empty string if this is not a validation request.

func ValidateNames

func ValidateNames(c *http.Context) []string

ValidateNames returns the individual field names being validated as a slice.

func Version

func Version(c *http.Context) string

Version returns the Unpoly version string from the request (X-Up-Version).

Types

type Plugin

type Plugin struct {
	nimbus.BasePlugin
}

Plugin integrates the Unpoly server protocol with Nimbus.

func New

func New() *Plugin

New creates a new Unpoly plugin instance.

func (*Plugin) DefaultConfig

func (p *Plugin) DefaultConfig() map[string]any

DefaultConfig returns the default configuration for the Unpoly plugin.

func (*Plugin) Middleware

func (p *Plugin) Middleware() map[string]router.Middleware

Middleware returns the named middleware provided by this plugin.

"unpoly" — the server protocol middleware. Register it as server middleware in start/kernel.go so every Unpoly request gets the required response headers.

Jump to

Keyboard shortcuts

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