Home

Awesome

inertia-echo

test MIT License Go Reference

The Inertia.js server-side adapter for Echo Go web framework.

Inertia.js is a JavaScript library that allows you to build a fully JavaScript-based single-page app without complexity. I assume that you are familiar with Inertia.js and how it works. You also need to familiarize yourself with Echo, a Go web framework. The inertia-echo assists you in developing web applications that leverage both of these technologies.

Installation

go get github.com/kohkimakimoto/inertia-echo

Minimum example

Please see Hello World example.

Usage

Shorthand routes

The inertia-echo provides a helper function for shorthand routes like Official Laravel Adapter.

e.GET("/about", inertia.Handler("About"))

See also the official document: Routing

Responses

Creating responses.

func ShowEventsHandler(c echo.Context) error {
	event := // retrieve a event...
	return inertia.Render(c, http.StatusOK, "Event/Show", map[string]interface{}{
		"Event": event,
	})
}

Root template data.

<meta name="twitter:title" content="{{ .page.Props.Event.Title }}">

Sometimes you may even want to provide data that will not be sent to your JavaScript component.

func ShowEventsHandler(c echo.Context) error {
	event := // retrieve a event...
	return inertia.RenderWithViewData(c, http.StatusOK, "Event/Show", map[string]interface{}{
		"Event": event,
	}, map[string]interface{}{
		"Meta": "Meta data...",
	})
}

You can then access this variable like a regular template variable.

<meta name="twitter:title" content="{{ .Meta }}">

See also the official document: Responses

Redirects

You can use Echo's standard way to redirect.

return c.Redirect(http.StatusFound, "/")

The following is a way to redirect to an external website in Inertia apps.

return inertia.Location(c, "/path/to/external")

See also the official document: Redirects

Shared data

Set shared data via middleware.

e.Use(inertia.MiddlewareWithConfig(inertia.MiddlewareConfig{
	Share: func(c echo.Context) (map[string]interface{}, error) {
		user := // get auth user...
		return map[string]interface{}{
			"AppName":  "App Name",
			"AuthUser": user,
		}, nil
	},
}))

Set shared data manually.

inertia.Share(c, map[string]interface{}{
	"AppName":  "App Name",
	"AuthUser": user,
})

See also the official document: Shared data

Partial reloads

inertia.Render(c, http.StatusOK, "Index", map[string]interface{}{
	// ALWAYS included on first visit
	// OPTIONALLY included on partial reloads
	// ALWAYS evaluated
	"users": users,

	// ALWAYS included on first visit...
	// OPTIONALLY included on partial reloads...
	// ONLY evaluated when needed...
	"users": func() (interface{}, error) {
		users, err := // get users...
		if err != nil {
			return nil, err
		}
		return users
	},

	// NEVER included on first visit
	// OPTIONALLY included on partial reloads
	// ONLY evaluated when needed
	"users": inertia.Lazy(func() (interface{}, error) {
		users, err := // get users...
		if err != nil {
			return nil, err
		}
		return users, nil
	}),
})

See also the official document: Partial reloads

Asset versioning

Configure asset version via middleware.

e.Use(inertia.MiddlewareWithConfig(inertia.MiddlewareConfig{
	VersionFunc: func() string { return version },
}))

Configure asset version manually.

inertia.SetVersion(c, func() string { return version })

See also the official document: Assset versioning

Server-side Rendering (SSR)

The inertia-echo supports SSR. Please see SSR Node.js example.

See also the official document: Server-side Rendering (SSR)

Unsupported features

Validation

The inertia-echo does not support validation, as Echo lacks built-in validation. The implementation of validation is up to you. If you wish to handle validation errors with inertia-echo, you will need to implement it yourself.

See also the official document: Validation

Demo application

Author

Kohki Makimoto kohki.makimoto@gmail.com

License

The MIT License (MIT)