Skip to content

Instantly share code, notes, and snippets.

@yogain123
Created May 5, 2020 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogain123/d4a25abe7d0a6f52e5c26902d21a5eca to your computer and use it in GitHub Desktop.
Save yogain123/d4a25abe7d0a6f52e5c26902d21a5eca to your computer and use it in GitHub Desktop.
SSR-React
@yogain123
Copy link
Author

yogain123 commented May 5, 2020

https://github.com/yogain123/React-SSR

Why SSR

CSR

Screen Shot 2020-05-05 at 11 11 59 PM

The entire goal of SSR is to make content visible to user as fast as possible

@yogain123
Copy link
Author

SSR Overview

Screen Shot 2020-05-05 at 11 16 52 PM
Screen Shot 2020-05-05 at 11 22 23 PM
Screen Shot 2020-05-05 at 11 22 42 PM

we still use react in the browser, we just defer loading up react app to be a little bit further along the process

@yogain123
Copy link
Author

Architecture Overview

Screen Shot 2020-05-05 at 11 30 27 PM



Screen Shot 2020-05-05 at 11 31 59 PM

@yogain123
Copy link
Author

The Render Function

Screen Shot 2020-05-06 at 12 28 42 AM

@yogain123
Copy link
Author

yogain123 commented May 6, 2020

The App we will be making

Screen Shot 2020-05-06 at 7 30 15 PM



Screen Shot 2020-05-06 at 7 31 23 PM



Screen Shot 2020-05-06 at 7 32 28 PM

@yogain123
Copy link
Author

Boilerplate setup

Screen Shot 2020-05-06 at 7 35 47 PM



Screen Shot 2020-05-06 at 7 36 12 PM

@yogain123
Copy link
Author

RenderToString Function

Screen Shot 2020-05-06 at 7 37 45 PM



Screen Shot 2020-05-06 at 7 38 45 PM

we will get error becoz node env has no idea what jsx is.

@yogain123
Copy link
Author

JSX on Server

How things work on CSR:

Screen Shot 2020-05-06 at 7 40 50 PM



The same can be achieved on the server by:

Screen Shot 2020-05-06 at 7 41 17 PM



Screen Shot 2020-05-06 at 7 43 24 PM
Screen Shot 2020-05-06 at 7 44 20 PM

@yogain123
Copy link
Author

yogain123 commented May 6, 2020

Server Side web pack

Screen Shot 2020-05-06 at 7 26 46 PM

running webpack, the above script will create a new folder with bundle.js inside it

node build/bundle.js will run the app



Screen Shot 2020-05-06 at 7 49 03 PM

@yogain123
Copy link
Author

Rebuilding and Restarting App

Screen Shot 2020-05-06 at 7 52 40 PM


  "scripts": {
    "dev:server": "nodemon --watch build --exec \"node build/bundle.js\"",
    "dev:build-server": "webpack --config webpack.server.js --watch",
  }

@yogain123
Copy link
Author

SSR, Isomorphic JavaScript, Universal JavaScript

Screen Shot 2020-05-06 at 7 54 22 PM

We can even do SSR without React, like using the handlebar, yeh Server Side templating/Rendering

SSR was not specific enough so we come up with IJ, UJ

Babel is also running in Server Side so we can also use New import way in Server as we do it in react

@yogain123
Copy link
Author

Events

Client side:
Screen Shot 2020-05-06 at 8 03 54 PM

BUT SSR will only send plain HTML page, no JS and all, so how any event will occur, like button click and all

Screen Shot 2020-05-06 at 8 07 03 PM

@yogain123
Copy link
Author

yogain123 commented May 6, 2020

React Router in SSR

Screen Shot 2020-05-06 at 8 24 37 PM



Screen Shot 2020-05-06 at 8 25 47 PM
Screen Shot 2020-05-06 at 8 26 05 PM

we will get an error with the above diagram, so

Screen Shot 2020-05-06 at 8 28 00 PM



Screen Shot 2020-05-06 at 8 28 42 PM



Screen Shot 2020-05-06 at 8 43 12 PM



Screen Shot 2020-05-06 at 8 41 43 PM



Screen Shot 2020-05-06 at 8 43 55 PM

@yogain123
Copy link
Author

yogain123 commented May 6, 2020

Redux with SSR

Screen Shot 2020-05-06 at 9 57 36 PM



Screen Shot 2020-05-07 at 12 00 33 AM

@yogain123
Copy link
Author

yogain123 commented May 8, 2020

Client-Side Rehydration

Screen Shot 2020-05-08 at 9 25 53 PM

The states are not preserver to client when communicated down to the browser

How to Solve?

Screen Shot 2020-05-08 at 9 29 30 PM
Screen Shot 2020-05-08 at 9 29 16 PM



Screen Shot 2020-05-08 at 9 35 40 PM



Screen Shot 2020-05-08 at 9 36 24 PM



Screen Shot 2020-05-08 at 9 36 48 PM

@yogain123
Copy link
Author

yogain123 commented May 8, 2020

Authentication Issue

CSR:

Screen Shot 2020-05-08 at 11 10 06 PM

SSR:

Screen Shot 2020-05-08 at 11 17 30 PM

The problem above might be node server and API server might be in different domain or subdomain, so because of CORS request and Auth will not work

So...

Screen Shot 2020-05-08 at 11 26 00 PM

Then why we are not using JWT??

Screen Shot 2020-05-08 at 11 31 53 PM

if you want to use authentication in SSR app then it has to be cookie-based so the auth details could be included in the initial request made to your server

@yogain123
Copy link
Author

yogain123 commented May 8, 2020

Adding Proxy

Screen Shot 2020-05-08 at 11 44 37 PM

whenever node server gets a request to .../api , it automatically sends off to http:// react-ssr-api.herokuapp.com

@yogain123
Copy link
Author

Adding an App Component

Screen Shot 2020-05-09 at 6 13 03 PM



Screen Shot 2020-05-09 at 6 13 47 PM

@yogain123
Copy link
Author

Better SEO using meta tag

Screen Shot 2020-05-10 at 10 43 55 PM

@yogain123
Copy link
Author

yogain123 commented May 10, 2020

@yogain123
Copy link
Author

yogain123 commented May 10, 2020

RenderToString vs RenderToNodeStream

RenderToString

Screen Shot 2020-05-11 at 1 49 51 AM

Screen Shot 2020-05-11 at 1 50 17 AM



RenderToNodeStream

Screen Shot 2020-05-11 at 1 50 03 AM
Screen Shot 2020-05-11 at 1 50 38 AM



Time to first byte (TTFB)
It is used by Google for indexing

But in case we use RenderToNodeSteam, but when it starts sending snipped of auth protected route, and then realize later it was not supposed to do that, then things will be problematic.

@yogain123
Copy link
Author

Screen Shot 2020-05-25 at 2 09 07 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment