reactssr is a package for rendering React applications.

reactssr

A Go package to perform Server Side Rendering of React apps.

Project status Build Status Go Report Card go.dev reference

Example usage

Given a bundle produced from an additional entrypoint to your application such as js/index.ssr.jsx:

import * as React from 'react';
import * as Server from 'react-dom/server'
import './index.css';
import App from './App';

const AppOutput = Server.renderToString(
  <React.StrictMode>
    <App />
  React.StrictMode>
);

// reactssr.render is the callback injected by the go runtime to pass the rendered application back.
reactssr.render(AppOutput);

This file should be bundled, for exaple, with esbuild as so:

npx esbuild \
   src/index.ssr.jsx \
   --inject:src/react-shim.js \
   --bundle \
   --sourcemap \
   --outfile=build/out.js \
   --loader:.js=jsx \
   --loader:.svg=text \
   --define:process.env.NODE_ENV=\"production\"

Then the following code will execute the bundle and load the results into a Go variable (for serving to a client, for emple).

r, _ := reactssr.NewServerSideRenderer("./testdata/test-app-1/build/out.js")
output, _ := r.Render()

// output contains the rendered html from the React application.

How this works

reactssr works by executing a React application bundle with reactssr.render injected into the global Javascript namespace.

In this example:

reactssr.render(Server.renderToString(
  <React.StrictMode>
    <App />
  </React.StrictMode>
));

reactssr.render is a Go callback which allows the Javascript execution environment to pass the rendered HTML and CSS between runtimes.

Performance

This package includes benchmarks which are run in CI: reactssr_test.go.

Recent performance results

go test -v -run=XXX -benchmem -bench=.*
goos: linux
goarch: amd64
pkg: github.com/tmc/reactssr
BenchmarkRender
BenchmarkRender-2    	     464	   5855720 ns/op	    3459 B/op	      19 allocs/op
PASS

This indicates that it takes just under 6 milliconds to render the current default output from create-react-app. This is without any specific work towards optimizing the implementation and this output is easily cachable.

Owner
Travis Cline
🚀
Travis Cline
Similar Resources

Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package.

Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package.

Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package. The library allows you to call Go service methods from PHP with a minimal footprint, structures and []byte support.

Dec 28, 2022

Gene parsing package for Axie Infinity

agp Package agp is a gene parsing package for Axie Infinity. The name agp stands for "Axie Gene Parser" which decodes the hex representation of an Axi

Apr 18, 2022

keeper is package for Go that provides a mechanism for waiting a result of execution function until context cancel.

keeper is package for Go that provides a mechanism for waiting a result of execution function until context cancel.

Apr 18, 2022

A lightweight casting package for Go projects

Cast GoLobby Cast is a lightweight casting package for Go projects. Documentation Required Go Versions It requires Go v1.11 or newer versions. Install

Dec 21, 2022

📋 cross-platform clipboard package that supports accessing text and image in Go (macOS/Linux/Windows/Android/iOS)

clipboard Cross platform (macOS/Linux/Windows/Android/iOS) clipboard package in Go import "golang.design/x/clipboard" Features Cross platform supports

Dec 24, 2022

Raspberry pi GPIO controller package(CGO)

Raspberry pi GPIO controller package(CGO)

GOPIO A simple gpio controller package for raspberrypi. Documentation Examples Installation sudo apt-get install wiringpi go get github.com/polarspet

Nov 24, 2022

Package strnaming is used to Convert string to camelCase, snake_case, kebab-case.

strnaming Package strnaming is used to Convert string to camelCase, snake_case, kebab-case. Contents strnaming Contents API Examples Install Quick sta

Oct 24, 2021

A Go package for checking conditions for slices and maps.

check Go package The check package of Go helps one to check various conditions for slices: []int []float64 []string []bool maps: map[string]int map[st

Aug 26, 2022

Simple go package which converts roman strings to integer

romanparse Simple go package which converts roman strings

Aug 11, 2022
Comments
  • Error with v8go

    Error with v8go

    # rogchap.com/v8go
    v8go.cc:11:10: fatal error: libplatform/libplatform.h: No such file or directory
       11 | #include "libplatform/libplatform.h"
          |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    compilation terminated.
    
  • material-ui: Show end to end example including material-ui CSS theming support.

    material-ui: Show end to end example including material-ui CSS theming support.

    Currently the exposed api allow enables delivery of HTML back to the Go runtime. Extending the possible return type from reactssr.render to allow passing CSS and demonstrating this end to end will provide a useful end to end example.

Related tags
Utility to restrict which package is allowed to import another package.

go-import-rules Utility to restrict which package is allowed to import another package. This tool will read import-rules.yaml or import-rules.yml in t

Jan 7, 2022
Golang source code parsing, usage like reflect package

gotype Golang source code parsing, usage like reflect package English įŽ€äŊ“中文 Usage API Documentation Examples License Pouch is licensed under the MIT Li

Dec 9, 2022
A Go preprocessor for package scoped reflection

pkgreflect - A Go preprocessor for package scoped reflection Problem: Go reflection does not support enumerating types, variables and functions of pac

Dec 13, 2022
Package ethtool allows control of the Linux ethtool generic netlink interface.

ethtool Package ethtool allows control of the Linux ethtool generic netlink interface.

Dec 14, 2022
Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite
Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite

go-testdeep Extremely flexible golang deep comparison, extends the go testing package. Latest news Synopsis Description Installation Functions Availab

Jan 5, 2023
A well tested and comprehensive Golang statistics library package with no dependencies.

Stats - Golang Statistics Package A well tested and comprehensive Golang statistics library / package / module with no dependencies. If you have any s

Dec 30, 2022
A computational topology package for gophers.
A computational topology package for gophers.

Simplices; simplicial complexes; simplicial chains; chain, cycle, boundary and homology groups; sets of simplices; methods for computing boundaries, Euler characteristics, Euler integrals, and Betti numbers, and more (with even more to come)!

Apr 19, 2021
Go package implementing Bloom filters

Go package implementing Bloom filters

Dec 30, 2022
go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

Jan 2, 2023
Go package to generate and manage color palettes & schemes 🎨
Go package to generate and manage color palettes & schemes 🎨

Go package to generate and manage color palettes & schemes

Dec 29, 2022