Next 13 - Server and Client components

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

With Next 13 the team made quite big changes regarding how components render. Not that it's all-new, but they seem to have simplified it and changed the defaults.
So let's take a closer look at how rendering works in Next 13.
We'll first have to address what is seen as the server and the client.
As you can see, the tipping point lies in where the calculations happen.
So what exactly did the ecosystem look like before Next 13?
In general, with React, everything happens on the client side. Next solve this by breaking your React components down into pages that could partially be rendered on the server. However, this generated HTML on the server, which had to be hydrated on the client again, meaning we needed extra JavaScript.
With the introduction of the clear distinguishment between server and client-side components, React can render on the server OR the client.
The new default in all this is server-side rendering.
With this addition of being Server side first, we get another two choices. Either we render statically or dynamically.
Let's take a closer look at what that means.
You might wonder, if these server components are the new default, how do I use client components?
And Next makes this super simple by adding a directive at the top of your file.
'use client';
export default function YourPage() {
//Client-side code
}
Yep, that directive will take care of everything!
It might feel tricky to know which one to use at which stage, so let's look at Next's directive on this.
Let's break it down by what you might need.
Reduce JavaScript code -> Server component
Add interactivity (click/change listeners) -> Client component
You might think, ok but surely they need to mix and match, and indeed they do.
The advice given by Next is that we opt to make component server-side where possible and extract the ones that need client-side rendering.
This way, our server components only need to render that specific small component on the client. But more on this later on when we try them out.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter