Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

⏩ Inertia.js server-side adapter for Go.

License

NotificationsYou must be signed in to change notification settings

petaki/inertia-go

Repository files navigation

Inertia GO

Inertia.js Go Adapter

Build StatusLicense: MIT

The Inertia.js server-side adapter for Go. Visitinertiajs.com to learn more.

Installation

Install the package using thego get command:

go get github.com/petaki/inertia-go

Usage

1. Create new instance

url:="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)

2. Register the middleware

mux:=http.NewServeMux()mux.Handle("/",inertiaManager.Middleware(homeHandler))

3. Render in handlers

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,})//...

4. Server-side Rendering (Optional)

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.

Examples

The following examples show how to use the package.

Share a prop (globally)

inertiaManager.Share("title","Inertia App Title")

Share a prop (context based)

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))    })}

Share a function with root template (globally)

inertiaManager.ShareFunc("asset",assetFunc)
<scriptsrc="{{ asset"js/app.js" }}"></script>

Share data with root template (globally)

inertiaManager.ShareViewData("env","production")
{{ if eq .env "production" }}    ...{{ end }}

Share data with root template (context based)

ctx:=inertiaManager.WithViewData(r.Context(),"meta",meta)r=r.WithContext(ctx)
<metaname="description"content="{{ .meta }}">

Root template

<!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>

Root template with Server-side Rendering (SSR)

<!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>

Example Apps

Satellite

Vite Vite /Vue3 Vue3

https://github.com/petaki/satellite

Homettp

Vite Vite /Vue3 Vue3

https://github.com/homettp/homettp

Waterkube

Vite Vite /Vue3 Vue3

https://github.com/waterkube/waterkube

Contributors

Reporting Issues

If you are facing a problem with this package or found any bug, please open an issue onGitHub.

License

The MIT License (MIT). Please seeLicense File for more information.

About

⏩ Inertia.js server-side adapter for Go.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp