reactssr is a package for rendering React applications.

Related tags

Utilities react go
Overview

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.

You might also like...
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.

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

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.

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

📋 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

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

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

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

Simple go package which converts roman strings to integer

romanparse Simple go package which converts roman strings

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.
    
    opened by Nebulizer1213 0
  • 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.

    opened by tmc 0
Releases(v0.1.0)
Owner
Travis Cline
🚀
Travis Cline
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

PAYFAZZ 0 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

null 47 Aug 31, 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

Erik Unger 100 Aug 29, 2022
Package ethtool allows control of the Linux ethtool generic netlink interface.

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

Matt Layher 47 Sep 7, 2022
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

Maxime Soulé 316 Sep 17, 2022
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

Montana Flynn 2.5k Sep 24, 2022
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)!

Raphael Reyna 10 Apr 19, 2021
Go package implementing Bloom filters

Go package implementing Bloom filters

Will Fitzgerald 1.7k Sep 21, 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.

Nick Snyder 2.1k Sep 20, 2022
Go package to generate and manage color palettes & schemes 🎨

Go package to generate and manage color palettes & schemes

Christian Muehlhaeuser 493 Sep 20, 2022