GithubHelp home page GithubHelp logo

eusoumaxi / react-ssr-sample-golang Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davehancock/react-ssr-sample-golang

0.0 0.0 0.0 392 KB

JavaScript 55.71% HTML 3.13% CSS 3.54% Go 33.47% Dockerfile 4.14%

react-ssr-sample-golang's Introduction

react-ssr-sample-golang

CircleCI

This project demonstrates rendering a react app on the server side using Golang.

TL;DR

The client side code consists of a small React app that uses some popular libraries such as react-router, bootstrap etc. It features a page that has dynamic data with state inserted from the server side which can then also be later updated on the client side.

The server side code consists of a simple Go application that renders JS with the help of Duktape and Go Duktape bindings. The server side app fetches some basic postcode data available from a third party, open API - https://api.postcodes.io.

Run

This sample has been packaged as a docker container and can be ran by executing:

docker run -p8080:8080 daves125125/react-ssr-sample-golang

Navigate to localhost:8080/ to see the sample running.

Build / Run from source

yarn install && yarn build && go run src/main.go

Or, via Docker:

yarn install && yarn build 
docker build -t test .
docker run -p8080:8080 test

How this works / Areas of interest

The JS code is split into two main bundles, the client.js and server.js. These are built as independent source sets by Webpack. Both the server.js and client.js depend upon the the main React App itself with the only difference being that the client side component includes client side specific code such as browser routing, and the server side code includes server side routing and injection of initial state.

The Server side uses Duktape to render only the server.js bundle which gets packaged as part of the build process.

Regarding SSR, the main files of interest are:

  • react-src/client.js
  • react-src/server.js
  • src/main.go (contains the entrypoint and routing logic)
  • src/render/engine.go (contains the rendering / binding logic to Duktape)

Performance

The below have been collected from repeated runs using the AB testing tool. This has been ran on a MacBook Pro (Retina, 13-inch, Early 2015)

At Rest / Startup Under Load
Render Speed (ms) ~60 ~60
Throughput (msgs/s) ~16 ~16
Memory Usage (Mb) ~9 ~20

Known TODOs

  • Refactoring needed on the Go sample code itself (few questionable areas and not so idiomatic)
  • Render speed is a little slower than expected, presumably because this is fresh evaluation of the script on each invocation (i.e no caching or up front compiling of the scripts)
  • Caching could be easily implemented, both on the templates and the server side state within the service.
  • Properly strip down webpack config and ejected create-react-app to barebones needed

react-ssr-sample-golang's People

Contributors

davehancock avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.