Yet another InertiaJS go adapter
Under heavy development, api can and will change at any point!
You can view an example here: https://github.com/tortlewortle/yaigo-example-vue
Since the client-side installation deviates a little bit from the normal setup starter kits will be made available for use. (soon)
Client side usage and instructions can be read here: https://inertiajs.com/
These docs are yet incomplete and not the clearest, it is recommended to take a peep at the example folder or basic examples!
go get go.tortle.tech/go-inertia
// base server with the frontend filesystem
inertiaServer, err = inertia.NewServer(frontend)
// with the vite dev server attached
inertiaServer, err = inertia.NewServer(frontend,
inertia.WithViteDevServer("http://localhost:5173"),
)
// with server side rendering (coming soon)
inertiaServer, err = inertia.NewServer(frontend,
inertia.WithSSR("http://127.0.0.1:13714", "bundle.mjs"),
)
You should wrap your servemux in the middleware to make use of the helper functions.
Without using the middleware you will not be able to benefit from the helper functions and will need to render using the InertiaServer instance.
http.ListenAndServe(":3000", inertiaServer.Middleware(mux))
You can provide props from middleware or within the handler using one of the helpers or providing them to the page directly.
func(w http.ResponseWriter, r *http.Request) {
inertia.SetProp(r, "username", "John Doe")
}
func(w http.ResponseWriter, r *http.Request) {
// using the helper
inertia.Render(w, r, "Welcome", nil)
// using your own instance
inertiaServer.Render(w, r, "Welcome", nil)
}
func(w http.ResponseWriter, r *http.Request) {
err := inertia.Render(w, r, "BenchPage", inertia.Props{
"username": "John Doe ",
})
}
func(w http.ResponseWriter, r *http.Request) {
err := inertia.Render(w, r, "BenchPage", inertia.Props{
// This request will take 50ms
// two "long-running" prop resolves, so we run them concurrently
"concurrentProp": inertia.Resolve(func() (any, error) {
timeOfRender := time.Now().Format(time.StampMilli)
time.Sleep(time.Millisecond * 50)
return timeOfRender, nil
}),
"concurrentProp2": inertia.Resolve(func() (any, error) {
time.Sleep(time.Millisecond * 50)
timeOfRender := time.Now().Format(time.StampMilli)
return timeOfRender, nil
}),
})
}
func(w http.ResponseWriter, r *http.Request) {
// this initial response will happen instantly and not wait.
// inertia will then fire off two separate requests to load each prop group.
err := inertia.Render(w, r, "BenchPage", inertia.Props{
// simple deferred prop, this request will take approx 500ms
"deferredProp": inertia.DeferSync(func() (any, error) {
time.Sleep(time.Millisecond * 500)
return "deferred prop", nil
}),
// three props part of a propgroup, these will be fetched in a separate request from the other deferred prop.
// this request will take approx 750ms even though we wait for a total of 2000ms
// This is because we execute the Defer() calls concurrently
// The DeferSync() callbacks are run after starting the Defer() callbacks
// This makes the total time 750ms instead of 2000ms
"deferredPropInGroup": inertia.Defer(func() (any, error) {
time.Sleep(time.Millisecond * 750)
return "one!", nil
}).Group("propgroup"),
"deferredPropInGroup2": inertia.Defer(func() (any, error) {
time.Sleep(time.Millisecond * 750)
return "two!", nil
}).Group("propgroup"),
// sync prop in same group as unsynced for extra dramatic effect
"deferredPropInGroup3": inertia.DeferSync(func() (any, error) {
time.Sleep(time.Millisecond * 500)
return "three!", nil
}).Group("propgroup"),
})
}
https://inertiajs.com/history-encryption
func encryptsHistory(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
inertia.EncryptHistory(r, true)
next.ServeHTTP(w, r)
})
}
http.ListenAndServe(":3000", inertiaServer.Middleware(encryptsHistory(mux)))
func(w http.ResponseWriter, r *http.Request) {
inertia.ClearHistory(r)
}
https://inertiajs.com/redirects
// redirects using status 303 to ensure GET requests
func(w http.ResponseWriter, r *http.Request) {
inertia.Redirect(w, r, "/")
}
// sends a 409 conflict with the X-Inertia-Location header.
func(w http.ResponseWriter, r *http.Request) {
inertia.Location(w, "https://google.com")
}
- SSR
- test coverage, the state of tests is sad right now
- test helpers for testing your own application
- frontend starter kits for vue, react and svelte
- more complete starter kits (with ssr).
- better way of detecting local dev mode for vite dev server configuration
I liked using InertiaJS and I want to use it with Go.
- Understand how InertiaJS works.
- Being able to use InertiaJS for quick project prototyping.
This package uses ideas from both romsar/gonertia and petaki/inertia-go.