Help us shape the future of Go! Take the 2026 Go Developer Survey

inertia

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 10 Imported by: 0

README

inertia-gin

inertia-gin is a basic Inertia.js adapter for Gin.

Features

This package currently supports rendering Inertia pages, with automatic asset version handling, and flash data. In short, it doesn't support every Inertia feature, but if you want a simple way to render Inertia pages with Gin, this will work.

Installation

Install inertia-gin
go get github.com/balboacodes/inertia-gin
Install Vite

Installation instructions for Vite can be found in their docs.

Install inertia.js

Installation instructions for Inertia can be found in their docs.

Update index.html

This uses the file created when setting up a new Vite project.

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
        <title>App</title>
    </head>
    <body>
        <div id="app"></div>
        <script type="module" src="/src/main.tsx"></script>
        <script data-page="app" type="application/json">
            {{ .data }}
        </script>
    </body>
</html>

Usage

Gin
package main

import (
	"github.com/balboacodes/inertia-gin"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()
	store := cookie.NewStore([]byte("your-secret-key"))
	router.Use(sessions.Sessions("mysession", store))

	inertia.Init(router)

	router.GET("/", func(context *gin.Context) {
		inertia.Render(context, "Home", gin.H{"title": "Home"})
	})

	router.POST("/", func(context *gin.Context) {
		// perform some task...

		inertia.Flash(context, "Success!")
		context.Redirect(302, "/")
	})

	router.Run()
}
Inertia

This setup uses React for the frontend.

// main.tsx
import { createInertiaApp } from "@inertiajs/react";

createInertiaApp({
  strictMode: true,
});

Create a pages directory in the src directory that was created during Vite setup.

// src/pages/Home.tsx
import { Head } from "@inertiajs/react";

export default function Home({ title }: { title: string }) {
  return (
    <>
      <Head title={title} />
      <h1>{title}</h1>
    </>
  );
}

Run npm run build to build your frontend assets. Then you can run go run . to start your app as usual. Your app will now be running on Inertia!

Documentation

Overview

Package inertia provides the functionality to use Inertia.js with Gin. https://inertiajs.com

Index

Constants

View Source
const (
	// Set to true to indicate this is an Inertia response.
	X_INERTIA string = "X-Inertia"
	// Used for external redirects when a 409 Conflict response is returned due to asset version mismatches. Triggers a
	// full window.location visit.
	X_INERTIA_LOCATION string = "X-Inertia-Location"
	// Set to X-Inertia to help browsers correctly differentiate between HTML and JSON responses.
	// Set to Precognition on all responses when the Precognition middleware is applied.
	VARY string = "Vary"
)

Represents the Inertia response headers.

Variables

This section is empty.

Functions

func Flash

func Flash(context *gin.Context, value any) error

Flash flashes a value to the current session with the default "_flash" key. Returns error if session was not saved.

func Init

func Init(router *gin.Engine)

Init initializes Inertia by attaching its global middleware, serving its static assets (serves dist/assets from /assets), and loading its dist/index.html file.

func Render

func Render(context *gin.Context, component string, props gin.H) error

Render renders an Inertia page. Returns error if the version was not retrieved or if the page was not JSON encoded.

Types

This section is empty.

Jump to

Keyboard shortcuts

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