Liking cljdoc? Tell your friends :D

Getting started

inertia-clj is a Ring adapter for Inertia.js v3. Ring handlers return page components and props, and a React, Vue or Svelte frontend renders them as a single-page app. You don't need a separate API.

It works with any Ring stack: Reitit, Compojure, http-kit, Jetty, sync or async handlers. Its only dependency is jsonista.

Install

;; deps.edn
io.github.keanedp/inertia-clj {:mvn/version "0.1.1"}

;; Leiningen (project.clj)
[io.github.keanedp/inertia-clj "0.1.1"]

Usage

(require '[inertia.core :as inertia])

(defn users-index [_req]
  (inertia/render "Users/Index"
    {:users       (fn [] (db/users))                       ; lazy: called only if sent
     :stats       (inertia/defer (fn [] (slow-stats)) "sidebar")
     :permissions (inertia/optional (fn [] (perms)))}))

(defn template [{:keys [head body page]} req]
  (str "<!DOCTYPE html><html><head>" head (vite-tags) "</head>"
       "<body>" body "</body></html>"))

(def app
  (-> handler
      (inertia/wrap-inertia {:version  asset-hash      ; string or 0-arity fn
                             :template template
                             :share    (fn [req] {:auth {:user (current-user req)}})})
      wrap-flash
      wrap-session))

body contains the <script data-page="app" type="application/json"> element and the <div id="app"> root. Put it in your layout wherever the app should mount. head holds any tags the SSR server returns.

Example app

examples/react-vite in the repository contains a Reitit + Jetty + React 19 app that exercises forms, validation, deferred and optional props, DELETE redirects, external redirects and SSR.

cd examples/react-vite
make vite            # terminal 1: Vite on :5173 (runs npm install first if needed)
make server          # terminal 2: app on http://localhost:3000
make server SSR=1    # or: with SSR for pages that opt in

Without make:

npm install
npm run dev          # terminal 1
clojure -M:run       # terminal 2; for SSR set SSR=1 in the environment first
                     # (PowerShell: $env:SSR = '1')

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close