Skip to content

Instantly share code, notes, and snippets.

@yogain123
Created May 5, 2020 17:38
Show Gist options
  • 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

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