Skip to content

Commit 1f620d6

Browse files
committed
Add fiber example, update readme, only return string from react renderer and rework how routeIDS are saved
1 parent 159bb50 commit 1f620d6

34 files changed

+729
-536
lines changed

README.md

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,81 @@
1313

1414
Go-SSR is a drop in plugin to **any** existing Go web framework to allow **server rendering** [React](https://react.dev/). It's powered by [esbuild](https://esbuild.github.io/) and allows for passing props from Go to React with **type safety**.
1515

16-
# Quickstart
16+
# 📜 Features
1717

18-
```go
19-
package main
18+
- Lightning fast compiling
19+
- Auto generated Typescript structs for props
20+
- Smart build caching system
21+
- Hot reloading
22+
- Simple error reporting
23+
- Production optimized
24+
- Drop in to any existing Go web server
2025

21-
import (
22-
"example.com/gin/models"
23-
"github.com/gin-gonic/gin"
24-
go_ssr "github.com/natewong1313/go-react-ssr"
25-
"github.com/natewong1313/go-react-ssr/pkg/config"
26-
"github.com/natewong1313/go-react-ssr/pkg/react_renderer"
27-
)
26+
<!-- _View more examples [here](github.com/natewong1313/go-react-ssr/examples)_ -->
27+
28+
# 🛠️ Getting Started
29+
30+
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](/examples/README.md) folder to see how projects are structured.
31+
32+
## ⚡️ Using pre-configured templates
2833

29-
func main() {
30-
g := gin.Default()
31-
32-
go_ssr.Init(config.Config{
33-
PropsStructsPath: "./models/props.go",
34-
})
35-
36-
g.GET("/", func(c *gin.Context) {
37-
react_renderer.RenderRoute(c, react_renderer.Config{
38-
File: "Home.tsx",
39-
Props: &models.IndexRouteProps{
40-
InitialCount: 0,
41-
},
42-
})
43-
})
44-
g.Run()
45-
}
34+
Templates have been pre-configured to be installed from the console using the [`gonew`](https://go.dev/blog/gonew) command. View more info in the [examples](/examples/README.md) folder
35+
36+
### Gin
37+
38+
```console
39+
$ gonew github.com/natewong1313/go-react-ssr/examples/gin example.com/gin
4640
```
4741

48-
# Installation
42+
### Fiber
43+
44+
```console
45+
$ gonew github.com/natewong1313/go-react-ssr/examples/fiber example.com/fiber
46+
```
4947

50-
### Installing Go-SSR
48+
## 📝 Add to existing web server
5149

52-
To install Go-SSR to an existing Go project, run the following Go command
50+
First, install the module
5351

54-
```bash
52+
```console
5553
$ go get -u github.com/natewong1313/go-react-ssr
5654
```
55+
56+
Then, add imports into your main file
57+
58+
```go
59+
import (
60+
...
61+
go_ssr "github.com/natewong1313/go-react-ssr"
62+
"github.com/natewong1313/go-react-ssr/pkg/config"
63+
"github.com/natewong1313/go-react-ssr/pkg/react_renderer"
64+
)
65+
```
66+
67+
In your main function, initialize the plugin
68+
69+
```go
70+
go_ssr.Init(config.Config{
71+
FrontendDir: "./frontend/src", // The React source files path
72+
GeneratedTypesPath: "./frontend/src/generated.d.ts", // Where the generated prop types will be created at
73+
PropsStructsPath: "./models/props.go", // Where the Go structs for your prop types are located
74+
})
75+
```
76+
77+
Once the plugin has been initialized, you can call the `react_renderer.RenderRoute` function to compile your React file to a string
78+
79+
```go
80+
g.GET("/", func(c *gin.Context) {
81+
c.Writer.Write(react_renderer.RenderRoute(react_renderer.Config{
82+
File: "Home.tsx", // The file name, located in FrontendDir
83+
Title: "My example app", // Set the app title
84+
MetaTags: map[string]string{ // Configure meta tags
85+
"og:title": "My example app",
86+
"description": "Example description",
87+
},
88+
Props: &models.IndexRouteProps{ // Pass in your props struct
89+
InitialCount: 0,
90+
},
91+
}))
92+
})
93+
```

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Examples

examples/fiber/assets/favicon.ico

4.19 KB
Binary file not shown.

examples/fiber/frontend/package-lock.json

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"dependencies": {
3+
"react": "^18.2.0",
4+
"react-dom": "^18.2.0"
5+
},
6+
"devDependencies": {
7+
"@types/react": "^18.2.21",
8+
"@types/react-dom": "^18.2.7",
9+
"typescript": "^5.2.2"
10+
}
11+
}
28 KB
Loading
186 KB
Loading
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
body {
2+
background-color: #242424;
3+
margin: 0 !important;
4+
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
5+
}
6+
7+
.home {
8+
height: 100vh;
9+
display: flex;
10+
justify-content: center;
11+
align-items: center;
12+
flex-direction: column;
13+
}
14+
15+
.img-container {
16+
display: flex;
17+
gap: 24px;
18+
align-items: center;
19+
}
20+
21+
h1 {
22+
padding-top: 20px;
23+
color: #fff;
24+
font-size: 42px;
25+
margin-bottom: 0;
26+
}
27+
28+
.counter-container {
29+
padding: 28px 0px;
30+
}
31+
32+
a {
33+
color: #888;
34+
text-decoration: none;
35+
}
36+
37+
a:hover {
38+
text-decoration: underline;
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useState } from "react";
2+
import { IndexRouteProps } from "./generated";
3+
import GoLogo from "../public/go.png";
4+
import ReactLogo from "../public/react.png";
5+
import "./Home.css";
6+
import Counter from "./components/Counter";
7+
8+
function Home({ initialCount }: IndexRouteProps) {
9+
const [count, setCount] = useState(initialCount);
10+
11+
return (
12+
<div className="home">
13+
<div className="img-container">
14+
<img src={GoLogo} alt="Go logo" height={70} width={170} />
15+
<img src={ReactLogo} alt="React logo" height={80} width={90} />
16+
</div>
17+
<h1>Go + React</h1>
18+
<div className="counter-container">
19+
<Counter count={count} increment={() => setCount(count + 1)} />
20+
</div>
21+
<a href="https://github.com/natewong1313/go-react-ssr" target="_blank">
22+
View project on GitHub
23+
</a>
24+
</div>
25+
);
26+
}
27+
28+
export default Home;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.counter {
2+
color: white;
3+
background-color: #1a1a1a;
4+
font-size: 16px;
5+
padding: 10px 16px;
6+
border: 2px solid transparent;
7+
border-radius: 8px;
8+
cursor: pointer;
9+
}
10+
11+
.counter:hover {
12+
border: 2px solid #06a8c5;
13+
}

0 commit comments

Comments
 (0)