DEV Community

K-Sato
K-Sato

Posted on

13 2 2 2 2

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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

nextjs tutorial video

Youtube Tutorial Series

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay