Overview
I tried implementing server-side rendering (SSR) with React in Go. Below is a demonstration and the repo.
While working on this, I discovered another project that basically dose what I wanted to achieve.
Go React SSR GitHub Repository
Objectives
- Perform SSR using React's renderToString in a Go environment.
- Implement hydration in the browser using React's hydrateRoot.
Tools Used
Firstly, I used rogchap/v8go to execute JavaScript in a Go environment. I listed other options below.
I added polyfills for the Web APIs used in the React code.
Also, I used esbuild (which is built in Go) for transpiling and bundling the React code.
Process
This is the process I followed:
- Use esbuild to build the React code into a form executable on both the server and client sides.
- Use rogchap/v8go to perform SSR (
renderToString
) on the server-side code. - Embed the SSR-generated content and the client-side code for hydration into an HTML template using Go’s standard library html/template (props are also injected here).
- Make the HTML accessible using Go's standard library net/http.
Result
Here is the code I ended up with. It should do what I intended to do in the Objectives section above.
Top comments (1)
Thanks for sharing the code, it is very helpful with block blast.