Skip to content

Build full-stack web apps with Go & React. End to end type-safety and lightning fast ⚡️

License

natewong1313/go-react-ssr

Repository files navigation


Go Report GoDoc MIT License

Go-SSR is a drop in plugin to any existing Go web framework to allow server rendering React. It's powered by esbuild and allows for passing props from Go to React with type safety.

Go-SSR was developed due to a lack of an existing product in the Go ecosystem that made it easy to build full-stack React apps. At the time, most Go web app projects were either built with a static React frontend with lots of client-side logic or html templates. I envisioned creating a new solution that would allow you to create full-stack Go apps with React but with logic being moved to the server and being able to pass that logic down with type-safe props. This project was inspired by Remix and Next.JS, but aims to be a plugin and not a framework.

📜 Features

  • Lightning fast compiling
  • Auto generated Typescript structs for props
  • Hot reloading
  • Simple error reporting
  • Production optimized
  • Drop in to any existing Go web server

🛠️ Getting Started

Go-SSR can either be installed by cloning a template or simply installing it on your own. It is reccomended to take a look at the examples folder to see how projects are structured.

⚡️ Using pre-configured templates

Templates have been pre-configured to be installed from the console using the gonew command. View more info in the examples folder

Gin

$ gonew github.com/natewong1313/go-react-ssr/examples/gin example.com/gin $ cd gin && go get github.com/natewong1313/go-react-ssr@latest

Fiber

$ gonew github.com/natewong1313/go-react-ssr/examples/fiber example.com/fiber $ cd fiber && go get github.com/natewong1313/go-react-ssr@latest

📝 Add to existing web server

First, install the module

$ go get -u github.com/natewong1313/go-react-ssr

Then, add imports into your main file

import ( ...	go_ssr "github.com/natewong1313/go-react-ssr" "github.com/natewong1313/go-react-ssr/config" "github.com/natewong1313/go-react-ssr/react_renderer" )

In your main function, initialize the plugin

go_ssr.Init(config.Config{ FrontendDir: "./frontend/src", // The React source files path GeneratedTypesPath: "./frontend/src/generated.d.ts", // Where the generated prop types will be created at PropsStructsPath: "./models/props.go", // Where the Go structs for your prop types are located })

Once the plugin has been initialized, you can call the react_renderer.RenderRoute function to compile your React file to a string

g.GET("/", func(c *gin.Context) { c.Writer.Write(react_renderer.RenderRoute(react_renderer.Config{ File: "Home.tsx", // The file name, located in FrontendDir Title: "My example app", // Set the app title MetaTags: map[string]string{ // Configure meta tags "og:title": "My example app", "description": "Example description",	}, Props: &models.IndexRouteProps{ // Pass in your props struct InitialCount: 0,	},	})) })

About

Build full-stack web apps with Go & React. End to end type-safety and lightning fast ⚡️

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages