DEV Community


Discussion on: Understanding Server Side Rendering

bbenefield89 profile image
Brandon Benefield

Hi @christopherkade , thanks for sharing your thoughts. I never put in the effort to play with SSR libs/frameworks until this morning after reading this post so thank you for that as well! I do have some questions about the drawbacks you mentioned concerning SSR.

📈 More server requests
Each new page requires a new server request. Although these are short and usually lightweight, it's important to keep it in mind.

Can this just be mitigated and no longer a concern if your "server", aka your Next.js app, was only handling the frontend: rendering and routing, while your actual server, let's just go with Express.js, handles your data: REST, CRUD operations, user auth, etc.? Of course, in this scenario both ends would be hosted on different machines.

🕹 Less interactivity
Imagine if Trello or Gmail were using SSR, you'd have to reload the page completely at each user interaction, which impacts the overall experience.

This one is a little confusing for me to understand. Is this suggesting that we can't use a fetch request to send or receive data and just replace the DOM content?

christopherkade profile image
Christopher Kade Author

Hi Brandon, I'm really glad the article pushed you to start working with SSR !

If I understood your first question correctly, you'd have an Express server that handles usual operations while having a client built using Next and server-rendered.

Either way, your individual pages would be queried to the server at each route. Doing REST, CRUD and authentication operations by querying your server is one thing, but getting the HTML on each route is another.

For your second question, I understand the confusion, so let me clarify:

Imagine you have an e-mail under /inbox/42 where 42 is the e-mail's ID. The index.html page related to this specific e-mail would need to be already rendered on the server in order for it to be SSR. This way, when you reach this page, the complete page (and therefore its content) would be received by the client.
In an SPA case, the data for the e-mail with an ID of 42 would be queried to the server as you'd normally do.

I really hope this helps, and thank you for your feedback !

bbenefield89 profile image
Brandon Benefield

Gotcha, definitely cleared up what I was thinking, thank you.

So I guess another question is what is technically SSR? You touch on it a bit in this post but with everything getting data from a RESTful endpoint this would typically mean even a simple blog grabbing a posts' content from a database wouldn't be considered SSR correct?

For example, if using Next and React, when requesting a blog post I would have a fetch request inside of the componentDidMount() method which is only called after the initial render() method. The way you've explained the Gmail scenario makes me think that this is no longer considered SSR even if using Next to render the page, correct? Another thought is that perhaps all of this is done on the server side, going through all of the React Component LifeCycle Methods before actually sending the HTML to the client which if that's the case then that would be considered SSR.

mohammedfoysal profile image
Mohammed Foysal

Is there a way to have a SSR SPA hybrid? So on the first request, it acquires a server rendered page and then all clicks after that work with a SPA which makes RESTful calls?

Thread Thread
matteorigon profile image
Matteo Rigon

Nuxt and Next already do this. You only reach the server on the first request and then everything else works like a spa

Thread Thread
devhammed profile image
Hammed Oyedele

Same with Gatsby.

Thread Thread
beenotung profile image
Beeno Tung

If you want to go futher, checkout LiveView, it returns complete layout in html/css on the initial get request, then use websocket to push user interaction to the server.
The server maintain the virtual dom, and send the diff patch to the client for partial update.

This way the client don't need to download heavy javascript bundles. It has two implementations so far, in typescript and elixir