- Notifications
You must be signed in to change notification settings - Fork14
⏩ Inertia.js server-side adapter for Go.
License
petaki/inertia-go
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The Inertia.js server-side adapter for Go. Visitinertiajs.com to learn more.
Install the package using thego get command:
go get github.com/petaki/inertia-gourl:="http://inertia-app.test"// Application URL for redirectrootTemplate:="./app.gohtml"// Root template, see the example belowversion:=""// Asset versioninertiaManager:=inertia.New(url,rootTemplate,version)
Or create withembed.FS for root template:
import"embed"//go:embed templatevartemplateFS embed.FS// ...inertiaManager:=inertia.NewWithFS(url,rootTemplate,version,templateFS)
mux:=http.NewServeMux()mux.Handle("/",inertiaManager.Middleware(homeHandler))
funchomeHandler(w http.ResponseWriter,r*http.Request) {// ...err:=inertiaManager.Render(w,r,"home/Index",nil)iferr!=nil {// Handle server error... }}
Or render with props:
// ...err:=inertiaManager.Render(w,r,"home/Index",map[string]interface{}{"total":32,})//...
First, enable SSR with the url of the Node server:
inertiaManager.EnableSsrWithDefault()// http://127.0.0.1:13714
Or with custom url:
inertiaManager.EnableSsr("http://ssr-host:13714")
This is a simplified example using Vue 3 and Laravel Mix.
// resources/js/ssr.jsimport{createInertiaApp}from'@inertiajs/vue3';importcreateServerfrom'@inertiajs/vue3/server';import{renderToString}from'@vue/server-renderer';import{createSSRApp,h}from'vue';createServer(page=>createInertiaApp({ page,render:renderToString,resolve:name=>require(`./pages/${name}`),setup({ App, props, plugin}){returncreateSSRApp({render:()=>h(App,props)}).use(plugin);}}));
The following config creates thessr.js file in the root directory, which should not be embedded in the binary.
// webpack.ssr.mix.jsconstmix=require('laravel-mix');constwebpackNodeExternals=require('webpack-node-externals');mix.options({manifest:false}).js('resources/js/ssr.js','/').vue({version:3,options:{optimizeSSR:true}}).webpackConfig({target:'node',externals:[webpackNodeExternals({allowlist:[/^@inertiajs/]})]});
You can find the example for the SSR based root template below. For more information, please read the official Server-side Rendering documentation oninertiajs.com.
The following examples show how to use the package.
inertiaManager.Share("title","Inertia App Title")
funcauthenticate(next http.Handler) http.Handler {returnhttp.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {// ...ctx:=inertiaManager.WithProp(r.Context(),"authUserID",user.ID)next.ServeHTTP(w,r.WithContext(ctx)) })}
inertiaManager.ShareFunc("asset",assetFunc)
<scriptsrc="{{ asset"js/app.js" }}"></script>
inertiaManager.ShareViewData("env","production")
{{ if eq .env "production" }} ...{{ end }}ctx:=inertiaManager.WithViewData(r.Context(),"meta",meta)r=r.WithContext(ctx)
<metaname="description"content="{{ .meta }}">
<!DOCTYPE html><html><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><linkhref="css/app.css"rel="stylesheet"><linkrel="icon"type="image/x-icon"href="favicon.ico"></head><body><divid="app"data-page="{{ marshal .page }}"></div><scriptsrc="js/app.js"></script></body></html>
<!DOCTYPE html><html><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><linkhref="css/app.css"rel="stylesheet"><linkrel="icon"type="image/x-icon"href="favicon.ico"> {{ if .ssr }} {{ raw .ssr.Head }} {{ end }}</head><body> {{ if not .ssr }}<divid="app"data-page="{{ marshal .page }}"></div> {{ else }} {{ raw .ssr.Body }} {{ end }}</body></html>
https://github.com/petaki/satellite
https://github.com/homettp/homettp
https://github.com/waterkube/waterkube
- @monstergron for logo (ArtStation)
If you are facing a problem with this package or found any bug, please open an issue onGitHub.
The MIT License (MIT). Please seeLicense File for more information.
About
⏩ Inertia.js server-side adapter for Go.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors2
Uh oh!
There was an error while loading.Please reload this page.
