DEV Community

K-Sato
K-Sato

Posted on

SSR React in Go

Overview

I tried implementing server-side rendering (SSR) with React in Go. Below is a demonstration and the repo.

Go SSR POC GitHub Repository

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:

  1. Use esbuild to build the React code into a form executable on both the server and client sides.
  2. Use rogchap/v8go to perform SSR (renderToString) on the server-side code.
  3. 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).
  4. 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.

Go SSR POC GitHub Repository

Top comments (0)