### Install Inertia.js Go Adapter Source: https://github.com/petaki/inertia-go/blob/master/README.md Command to install the Inertia.js server-side adapter for Go using the `go get` command. ```Go go get github.com/petaki/inertia-go ``` -------------------------------- ### Example Inertia Root Template Source: https://github.com/petaki/inertia-go/blob/master/README.md A basic HTML root template for an Inertia.js application. It includes essential meta tags, links to CSS, and the `div#app` element where Inertia will mount and render the frontend components. The `data-page` attribute is crucial for Inertia's client-side hydration. ```HTML
``` -------------------------------- ### Create New Inertia Manager Instance in Go Source: https://github.com/petaki/inertia-go/blob/master/README.md Initializes a new Inertia manager instance with the application URL, root template path, and an optional asset version. This is the standard way to set up the Inertia adapter. ```Go url := "http://inertia-app.test" // Application URL for redirect rootTemplate := "./app.gohtml" // Root template, see the example below version := "" // Asset version inertiaManager := inertia.New(url, rootTemplate, version) ``` -------------------------------- ### Create Inertia Manager Instance with Embedded File System in Go Source: https://github.com/petaki/inertia-go/blob/master/README.md Initializes a new Inertia manager instance using `embed.FS` for the root template. This allows templates to be embedded directly into the Go binary, simplifying deployment. ```Go import "embed" //go:embed template var templateFS embed.FS // ... inertiaManager := inertia.NewWithFS(url, rootTemplate, version, templateFS) ``` -------------------------------- ### Configure Inertia SSR Entry Point with Vue 3 Source: https://github.com/petaki/inertia-go/blob/master/README.md JavaScript code for the Inertia.js SSR entry point (`ssr.js`), using `createInertiaApp` and `createServer` with Vue 3 for server-side rendering. This file is processed by a Node.js environment to pre-render Inertia pages. ```JavaScript // resources/js/ssr.js import { createInertiaApp } from '@inertiajs/vue3'; import createServer from '@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 }) { return createSSRApp({ render: () => h(App, props) }).use(plugin); } })); ``` -------------------------------- ### HTML Root Template for Inertia.js with Server-Side Rendering Source: https://github.com/petaki/inertia-go/blob/master/README.md This HTML template serves as the base layout for an Inertia.js application integrated with a Go backend. It conditionally renders content based on whether server-side rendering (SSR) is active, embedding either raw SSR output (head and body) or client-side page data for hydration. It includes standard HTML boilerplate, meta tags, and links to CSS and a favicon. ```html {{ if .ssr }} {{ raw .ssr.Head }} {{ end }} {{ if not .ssr }} {{ else }} {{ raw .ssr.Body }} {{ end }} ``` -------------------------------- ### Context-Based View Data Sharing with Root Template in Go Source: https://github.com/petaki/inertia-go/blob/master/README.md Shares data with the root template for a specific request context. This allows dynamic template content based on the current request, such as meta descriptions or page-specific titles. ```Go ctx := inertiaManager.WithViewData(r.Context(), "meta", meta) r = r.WithContext(ctx) ``` ```HTML ``` -------------------------------- ### Configure Webpack Mix for Inertia SSR Build Source: https://github.com/petaki/inertia-go/blob/master/README.md Webpack Mix configuration for building the Inertia.js SSR bundle. It targets Node.js and uses `webpack-node-externals` to prevent bundling node modules, ensuring the SSR bundle is lightweight and executable in a Node.js environment. ```JavaScript // webpack.ssr.mix.js const mix = require('laravel-mix'); const webpackNodeExternals = 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/ ] }) ] }); ``` -------------------------------- ### Enable Server-Side Rendering (SSR) in Go Source: https://github.com/petaki/inertia-go/blob/master/README.md Configures the Inertia manager to enable Server-Side Rendering (SSR). This allows the initial page load to be rendered on the server, improving SEO and perceived performance. You can use a default SSR URL or specify a custom one. ```Go inertiaManager.EnableSsrWithDefault() // http://127.0.0.1:13714 ``` ```Go inertiaManager.EnableSsr("http://ssr-host:13714") ``` -------------------------------- ### Render Inertia Page in Go Handler Source: https://github.com/petaki/inertia-go/blob/master/README.md Renders an Inertia page from a Go HTTP handler. The `Render` method takes the HTTP response writer, request, the frontend component name, and optional props. It handles sending the correct Inertia response. ```Go func homeHandler(w http.ResponseWriter, r *http.Request) { // ... err := inertiaManager.Render(w, r, "home/Index", nil) if err != nil { // Handle server error... } } ``` ```Go // ... err := inertiaManager.Render(w, r, "home/Index", map[string]interface{}{ "total": 32, }) //... ``` -------------------------------- ### Globally Share a Prop with Inertia in Go Source: https://github.com/petaki/inertia-go/blob/master/README.md Shares a key-value pair globally with all Inertia responses. This data will be available as a prop on the frontend for every page rendered by Inertia. ```Go inertiaManager.Share("title", "Inertia App Title") ``` -------------------------------- ### Globally Share a Function with Root Template in Go Source: https://github.com/petaki/inertia-go/blob/master/README.md Shares a Go function globally with the root template. This allows the function to be called directly from the HTML template, useful for common utilities like asset URL generation. ```Go inertiaManager.ShareFunc("asset", assetFunc) ``` ```HTML ``` -------------------------------- ### Globally Share Data with Root Template in Go Source: https://github.com/petaki/inertia-go/blob/master/README.md Shares a key-value pair globally with the root template. This data is accessible for rendering template-specific content, such as environment variables or global settings. ```Go inertiaManager.ShareViewData("env", "production") ``` ```HTML {{ if eq .env "production" }} ... {{ end }} ``` -------------------------------- ### Context-Based Prop Sharing with Inertia in Go Source: https://github.com/petaki/inertia-go/blob/master/README.md Shares a prop with Inertia for a specific request context. This is typically used within middleware to add request-specific data (e.g., authenticated user ID) that should only be available for the current request. ```Go func authenticate(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // ... ctx := inertiaManager.WithProp(r.Context(), "authUserID", user.ID) next.ServeHTTP(w, r.WithContext(ctx)) }) } ``` -------------------------------- ### Register Inertia Middleware in Go Source: https://github.com/petaki/inertia-go/blob/master/README.md Registers the Inertia manager's middleware with an HTTP router (e.g., `http.NewServeMux`), wrapping a handler function. This middleware handles Inertia-specific request headers and responses. ```Go mux := http.NewServeMux() mux.Handle("/", inertiaManager.Middleware(homeHandler)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.