inertia

package module
v0.0.0-...-c586633 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 9 Imported by: 0

README

Inertia.js Go (GoFiber) Adapter

Inertia-Fiber uses the GoFiber view engine as default.

The Inertia.js server-side adapter for Go. Visit inertiajs.com to learn more.
Note: All features have been tested in the example project. If, however, you do find a bug, please report it in the issues.

[!WARNING] This code does not yet support TypeScript and ReactJS, only supports VueJS and Vite.

Usage

package main

import (
    "github.com/gofiber/fiber/v2"
    "github.com/theartefak/inertia-fiber"
)

func main() {
    engine := inertia.New()
    app := fiber.New(fiber.Config{
        Views: engine,
    })

    app.Use(engine.Middleware())

    app.Get("/", func(c *fiber.Ctx) error {
        return engine.View("Index", fiber.Map{
            "greeting": "Hello World",
        })
    }).Name("Home")

    app.Listen("127.0.0.1:8000")
}
And then in the app.html
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />

        <title inertia>Artefak</title>
        {{ .Vite }}
        {{ .Ziggy }}
    </head>
    <body>
        {{ .Inertia }}
    </body>
</html>
Example with config
...
//go:embed *
var fs embed.FS

func main() {
    engine := inertia.New(inertia.Config{
        FS         : http.FS(fs),         // The file system to use for loading templates and assets.
        AssetsPath : "./resources/js",    // The path to the assets directory.
        Template   : "Index",             // The name of the template to use.
    })
...

For more examples refer to this repo at https://github.com/theartefak/demo-inertia-fiber

Default Config
  • Root : ./resources/views
  • FS : None (You can use embed.FS and then call http.FS on it.)
  • AssetsPath : ./resources/js
  • Template : app
Extra functions :
  • Share : Shares a prop for every request.
  • WithProp : Add a prop from middleware for the next request.
  • WithViewData : Share a param with the root template.

Credits

and many more.

License

This open-sourced software is licensed under the MIT license.

Documentation

Index

Constants

View Source
const (
	HeaderPrefix           = "X-Inertia"
	HeaderVersion          = HeaderPrefix + "-Version"
	HeaderLocation         = HeaderPrefix + "-Location"
	HeaderPartialData      = HeaderPrefix + "-Partial-Data"
	HeaderPartialComponent = HeaderPrefix + "-Partial-Component"
)

The Base "X-Inertia" header prefixes

Variables

View Source
var DefaultConfig = Config{
	Root:       "resources/views",
	AssetsPath: "resources/js",
	Template:   "app",
}

DefaultConfig is the default config

Functions

This section is empty.

Types

type Config

type Config struct {
	Root       string          // The root directory of the application.
	FS         http.FileSystem // The file system to use for loading templates and assets.
	AssetsPath string          // The path to the assets directory.
	Template   string          // The name of the template to use.
}

Config represents the configuration for the Inertia engine.

type Engine

type Engine struct {
	*html.Engine
	// contains filtered or unexported fields
}

Engine represents the Inertia engine.

func New

func New(config ...Config) *Engine

New creates a new instance of the Engine.

func (*Engine) Middleware

func (e *Engine) Middleware() fiber.Handler

Middleware returns a middleware function that sets the version header and context for the engine.

func (*Engine) Share

func (e *Engine) Share(name string, value any)

func (*Engine) View

func (e *Engine) View(component string, props fiber.Map, context *fiber.Ctx) error

View function returns a Fiber handler function for rendering Inertia.js views.

func (*Engine) WithProp

func (e *Engine) WithProp(name string, value any)

func (*Engine) WithViewData

func (e *Engine) WithViewData(name string, value any)

type Page

type Page struct {
	Component string                 `json:"component"`
	Props     map[string]interface{} `json:"props"`
	URL       string                 `json:"url"`
	Version   string                 `json:"version"`
}

Page type.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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