http_inertia

http_inertia is a server-agnostic Inertia protocol adapter for Gleam. It works with gleam/http request values and supplies the server-side pieces needed to build Inertia responses: page-object encoding, initial-page markup, request inspection, and prop-selection helpers.

It does not depend on a particular HTTP server or response type. Your web framework remains responsible for turning the generated page data into an HTTP response and serving the Inertia client assets.

Installation

gleam add http_inertia

Creating a page

Create a Page with a component name, JSON props, and an asset version. Encode it with page_component_json when returning an Inertia response.

import gleam/json
import http_inertia
let page =
http_inertia.page(
component: "users/index",
props: [
#("users", json.array(["Ada"], of: json.string)),
#("errors", json.object([])),
],
version: http_inertia.StringVersion("asset-version"),
)
let page_json = http_inertia.page_component_json("/users", page)

For the initial non-Inertia visit, app_script produces the #app mount element and the JSON page-data script expected by the Inertia client:

let elements = http_inertia.app_script("/users", page)

Request helpers

Use is_inertia_request to distinguish an Inertia visit from an initial page load. request_url returns the path and query string for the current request.

For partial reloads, use is_partial_reload_for together with should_include_prop before doing work to load optional props:

let include_permissions =
http_inertia.is_partial_reload_for(req, "users/index")
&& http_inertia.should_include_prop(req, "users/index", "permissions")

should_skip_once_prop applies the corresponding request rules for props registered with once_prop and with_once_props.

Protocol metadata

Start with page, then use the with_* functions to attach optional Inertia metadata:

See the http_inertia module documentation for each function's API and the Mist example for a complete server integration.

Further resources

Acknowledgements

wisp_inertia by Keuller Magalhaes inspired this package.