inertia

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: 8 Imported by: 0

README

Inertia.js Plugin for Nimbus

Integrates Inertia.js with Nimbus, enabling you to build single-page apps using Vue, React, or Svelte without building an API.

Installation

The plugin uses petaki/inertia-go as the server adapter:

go get github.com/petaki/inertia-go

Setup

1. Register the plugin
// bin/server.go
import "github.com/CodeSyncr/nimbus/plugins/inertia"

app.Use(inertia.New(inertia.Config{
    URL:          "http://localhost:3000",
    RootTemplate: "resources/views/app.html", // or leave empty for embedded default
    Version:      "1",
}))
2. Create the root template

Create resources/views/app.html (or use the embedded default):

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>My App</title>
  <link href="/css/app.css" rel="stylesheet">
</head>
<body>
  <div id="app" data-page="{{ marshal .page }}"></div>
  <script src="/js/app.js"></script>
</body>
</html>
3. Set up the frontend

Install Inertia and your framework (Vue, React, or Svelte):

npm install @inertiajs/vue3 vue  # or @inertiajs/react, @inertiajs/svelte

Create resources/js/app.js:

import { createApp } from 'vue'
import { createInertiaApp } from '@inertiajs/vue3'

createInertiaApp({
  resolve: name => {
    const pages = import.meta.glob('./Pages/**/*.vue')
    return pages[`./Pages/${name}.vue`]()
  },
  setup({ el, App, props, plugin }) {
    createApp(App).use(plugin).mount(el)
  },
})
4. Render Inertia pages in handlers
func (c *HomeController) Index(ctx *http.Context) error {
    users := loadUsers()
    return inertia.Render(ctx, "Home/Index", map[string]any{
        "users": users,
    })
}

The component name (Home/Index) maps to Pages/Home/Index.vue in your frontend.

Configuration

Option Description Default
URL Application URL for redirects http://localhost:3000
RootTemplate Path to root HTML template Embedded default
TemplateFS Optional embed.FS for root template -
Version Asset version for cache busting 1
SSRURL Node SSR server URL (optional) -

Server-Side Rendering (SSR)

For SSR with Vue/React, run a Node SSR server and set SSRURL:

app.Use(inertia.New(inertia.Config{
    URL:     "http://localhost:3000",
    Version: "1",
    SSRURL:  "http://127.0.0.1:13714",
}))

Notes

  • Inertia vs Unpoly: Use Inertia when you want a Vue/React/Svelte SPA with server-driven routing. Use Unpoly for server-rendered HTML with progressive enhancement.
  • The plugin wraps the HTTP server handler, so it runs before the router for every request.
  • Component names use Path/To/Component format, matching your frontend page structure.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(c *http.Context, component string, props map[string]any) error

Render renders an Inertia page. Use in handlers to return Vue/React/Svelte component data. When the request has the X-Inertia header, returns JSON. Otherwise returns full HTML with the root template.

func (c *HomeController) Index(ctx *http.Context) error {
    users := loadUsers()
    return inertia.Render(ctx, "Home/Index", map[string]any{
        "users": users,
    })
}

func Share

func Share(key string, value any)

Share shares a prop globally for all Inertia responses. WARNING: Use only for static data that never changes per-request. For request-specific data (user, flash, etc.), use ShareProp instead.

func ShareProp

func ShareProp(c *http.Context, key string, value any)

ShareProp shares a prop only for the current request. Use this in middleware or handlers to share data like the authenticated user or flash messages without affecting other concurrent requests.

Types

type Config

type Config struct {
	// URL is the application URL (e.g. "http://localhost:3000").
	// Used for redirects and asset URLs.
	URL string

	// RootTemplate is the path to the root HTML template.
	// Must contain a div with id="app" and data-page="{{ marshal .page }}".
	// Example: "resources/views/app.html"
	// If empty and TemplateFS is nil, uses the embedded default.
	RootTemplate string

	// TemplateFS is an optional embed.FS for the root template.
	// When set, RootTemplate is relative to this filesystem.
	TemplateFS fs.FS

	// Version is the asset version for cache busting.
	// Change this when assets change to force client reload.
	Version string

	// SSRURL is the optional Server-Side Rendering Node server URL.
	// When set, Inertia will use SSR for initial page load.
	SSRURL string
}

Config holds Inertia.js plugin configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults for development.

type Manager

type Manager interface {
	// Middleware returns http middleware that wraps the given handler.
	Middleware(next interface{}) interface{}
	// Render writes an Inertia page response to the response writer.
	Render(w interface{}, r interface{}, component string, props map[string]any) error
	// Share shares a prop globally for all responses.
	Share(key string, value any)
}

Manager is the Inertia adapter interface. Implementations wrap github.com/petaki/inertia-go or similar adapters.

type Plugin

type Plugin struct {
	nimbus.BasePlugin
	// contains filtered or unexported fields
}

Plugin integrates Inertia.js with Nimbus.

func New

func New(cfg Config) *Plugin

New creates a new Inertia plugin with the given config.

func (*Plugin) Boot

func (p *Plugin) Boot(app *nimbus.App) error

Boot initializes the Inertia manager and wraps the server handler.

func (*Plugin) Register

func (p *Plugin) Register(app *nimbus.App) error

Register binds the Inertia manager to the container (no-op for now).

Jump to

Keyboard shortcuts

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