Learning React with Ben Awad — 09

Photo by Pixabay from Pexels

This blog post originally posted here.

Now we are going to set up the Server-Side Rendering(SSR). To do that add the below package to the Web App project.

Now, we are bit refactoring our code and adding SSR to our urqlClient . Let’s create a few helper functions and move these things.

The next step is, use this withUrqlClient function in the index page to provide our urqlClient. Let’s go to Index.tsx file and make the changes.

Now you will get this error.

To fix this add as const to your credentials because we need to have it as a read-only property.

By default, this is not the SSR. We need to enable it by adding ssr: true .

Now we are adding Posts query.

Once we added it, first we try without SSR. To do that first get the Posts data and show in Index.tsx page.

Once we get the data we first check that data is there, by below code block.

To demonstrate the functionality without SSR, first, we created a sleep function in the server and show slow loading. It will take 3 seconds to load the data. But, if you check the source of the page, you will see a loading div in the source.

Now, let’s enable the SSR by adding the below code.

As a rule of thumb, use SSR for dynamic data loading. Also check that you are using urlq queries and mutations in there.

Now, let's wrap the login page and register page with withUrqlClient to not to use SSR.

Now we come to NavBar. We don’t want to run this on the server, because data will be null while rendering on the server. Because we are not passing the cookie in here.

First, we add the pause: true in NavBar. We define this true or false using window object.

In the server, this will be undefined. So, it will not process this mutation inside the server.

I will wrap up this post from here. If you have anything to ask regarding this please leave a comment here. Also, I wrote this according to my understanding. So if any point is wrong, don’t hesitate to correct me. I really appreciate you.

That’s for today friends. See you soon. Thank you.

References:

This article series based on the Ben Award — Fullstack React GraphQL TypeScript Tutorial. This is an amazing tutorial and I highly recommend you to check that out.

Full Stack Developer | AWS Associate Solution Architect | Skater